Clippy lints / Correctness
Clippy lint · Correctness · checked with Clippy 0.1.99
clippy::bad_bit_mask: 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: ComparisonBit OpExampleis alwaysFormula == or !=&x & 2 == 3falsec & m != c < or >=&x & 2 < 3truem < c > or <=&x & 1 > 1falsem <= c == or !=|x | 1 == 0falsec | m != c < or >=|x | 1 < 1falsem >= c <= or >|x | 1 > 0truem > c
What it meansChecks 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: ComparisonBit OpExampleis alwaysFormula == or !=&x & 2 == 3falsec & m != c < or >=&x & 2 < 3truem < c > or <=&x & 1 > 1falsem <= c == or !=|x | 1 == 0falsec | m != c < or >=|x | 1 < 1falsem >= c <= or >|x | 1 > 0truem > c It is in the correctness group and denied by default (the build fails).
Does it matter?
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.
Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
if (x & 1 == 2) { }
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::bad_bit_mask)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
bad_bit_mask = "allow"One run
cargo clippy -- -A clippy::bad-bit-maskThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
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) { }
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- almost_swappedChecks for foo = bar; bar = foo sequences
- approx_constantapproximate value of `f{32, 64}::consts::PI` found
- async_yields_asyncan async construct yields a type which is itself awaitable
- cast_slice_different_sizescasting between raw pointers to `[i32]` (element size 4) and `[u8]` (element size 1) does not adjust the count
- char_indices_as_byte_indicesChecks for usage of a character position yielded by .chars().enumerate() in a context where a byte index is expected, such as an argument to a specific str method or indexing into a str or String
- deprecated_semverthe since field must contain a semver-compliant version
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::almost_swappedChecks for foo = bar; bar = foo sequencesRead it next →Did this answer your question?