Struct clippy::bit_mask::BitMask
[−]
[src]
pub struct BitMask;
Checks for incompatible bit masks in comparisons, e.g. x & 1 == 2
.
This cannot work because the bit that makes up the value two was
zeroed out by the bit-and with 1. So 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 |
This lint is deny by default
There is also a lint that warns on ineffective masks that is warn by default.
|Comparison|Bit-Op |Example |equals |Formula|
|>
/ <=
||
/ ^
|x | 2 > 3
|x > 3
|¹ && m <= c
|
|<
/ >=
||
/ ^
|x ^ 1 < 4
|x < 4
|¹ && m < c
|
¹ power_of_two(c + 1)