A comparison is an operand of a bitwise operator without parentheses
What it finds
flags & MASK == 0: comparisons bind tighter than &, | and ^, so this is flags & (MASK == 0), a test of the lowest bit against the result of a comparison. The bits were meant to be compared: (flags & MASK) == 0.
One operand must be a comparison written without parentheses and the other something that is not already true or false, such as another comparison: a == b & c == d combines two truth values without short-circuiting, which is done on purpose. Where & follows a name in parentheses, as in (ulong)&x, the parentheses may be a cast to a type declared in a header, and & the address operator.
From the rule's source, bitwise_precedence.rs.
#define MASK 0x0f int test(unsigned flags, int a, int b, int c, char *p){ int r = 0; if (flags & MASK == 0)tx0 reports: MASK == 0 is compared before & is applied r++; if (a == b | c)tx0 reports: a == b is compared before | is applied r++; r += flags ^ a != b;tx0 reports: a != b is compared before ^ is applied if ((flags & MASK) == 0) r++; if (a == b & c == 1) r++; if (a < b | !c) r++; if ((unsigned long)p < (unsigned long)&r) r++; if ((long)p < (word)&r) 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:bitwise-precedence" = "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:bitwise-precedence] reason="..."tx0 explain tx0/c:bitwise-precedence shows the same in the terminal. Found a false positive? Report it.