A masked value is compared with a constant it can never equal, or always equals

tx0/java:impossible-maskdefaultwarningbug

What it finds

(flags & 0x4) == 0x8 is never true: the mask keeps only bit 2, so the result cannot have bit 3 set. Likewise (flags | 0x4) == 0x8 is never true, since the result always has bit 2, and (flags & 0) == 0 is always true. Either the mask or the constant is wrong.

Reported for == and != between a bitwise & or | with an integer literal and another integer literal, when the outcome follows from the constants alone.

From the rule's source, impossible_mask.rs.

Example from the rule's tests
class Masks {  boolean check(int flags) {    boolean a = (flags & 0x4) == 0x8;tx0 reports: (flags & 0x4) == 0x8 is always false    boolean b = (flags | 0x4) != 0x8;tx0 reports: (flags | 0x4) != 0x8 is always true    boolean c = (flags & 0) == 0;tx0 reports: (flags & 0) == 0 is always true    boolean d = (flags & 0xC) == 0x8;    boolean e = (flags | 0x4) == 0xC;    return a || b || c || d || e;  }}
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/java:impossible-mask" = "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/java:impossible-mask] reason="..."

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