The result of & or | with a constant is compared with a value it can never equal

tx0/c:impossible-mask-comparisondefaultwarningreliability/control-flowdepth fastbug, cwe-570

What it finds

(flags & 0x0f) == 0x10 or (mode | 4) == 2: the value compared with has bits that the masked side can never have, or lacks bits it always has, so the comparison is always false, or with != always true. Usually the mask or the value is wrong.

The mask and the value must be constants written with literals and not negative.

From the rule's source, impossible_mask_comparison.rs.

Example from the rule's tests
int flags_set(int flags){  if ((flags & 0x0f) == 0x10)tx0 reports: the result of & with 0xf can never equal 0x10, so this is always false    return 1;  if ((flags | 4) != 2)tx0 reports: always true    return 2;  return (flags & 0x0f) == 0x01;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

tx0.toml
[rules]
"tx0/c:impossible-mask-comparison" = "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:impossible-mask-comparison] reason="..."

tx0 explain tx0/c:impossible-mask-comparison shows the same in the terminal. Found a false positive? Report it.