A condition assigns a constant where a comparison was meant
What it finds
if (x = 1): the condition assigns a constant, so it is always true, or always false for zero, and the variable is overwritten. A comparison, x == 1, was meant.
The assignment must be the whole condition of an if, while, do or for, without extra parentheses, which mark an assignment as meant, and the value an integer or character literal. A fix to == is offered, but not applied without asking, since it changes what the program does.
From the rule's source, constant_assignment_condition.rs.
int next(void); int f(int x, int *p){ int r = 0; if (x = 1)tx0 reports: the condition assigns 1 to x instead of comparing them r++; while (*p = 0)tx0 reports: the condition assigns 0 to *p r++; do { r++; } while (x = -1);tx0 reports: the condition assigns -1 to x for (int i = 0; i = 'a'; i++)tx0 reports: the condition assigns 'a' to i r++; if ((x = 2)) r++; if (x = next()) r++; if (x == 3) r++; for (x = 0; x < 3; x++) r++; return r;} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/c:constant-assignment-condition" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/c:constant-assignment-condition] reason="..."tx0 explain tx0/c:constant-assignment-condition shows the same in the terminal. Found a false positive? Report it.