RS.CLIPPY.BAD_BIT_MASK

Expressions of the form `_ & mask == select` that will only ever return `true` or `false`

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: bad_bit_mask. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for incompatible bit masks in comparisons.

The formula for detecting if an expression of the type _ <bit_op> m <cmp_op> c (where <bit_op> is one of {&, |} and <cmp_op> is one of {!=, >=, >, !=, >=, >}) can be determined from the following table:

Comparison Bit Op Example is always Formula
== or != & x & 2 == 3 false c & m != c
< or >= & x & 2 < 3 true m < c
> or <= & x & 1 > 1 false m <= c
== or != | x | 1 == 0 false c | m != c
< or >= | x | 1 < 1 false m >= c
<= or > | x | 1 > 0 true m > c

Why is this bad?

If the bits that the comparison cares about are always set to zero or one by the bit mask, the comparison is constant true or false (depending on mask, compared value, and operators).

So the code is actively misleading, and the only reason someone would write this intentionally is to win an underhanded Rust contest or create a test-case for this lint.

Example

if (x & 1 == 2) { }