summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/bad_bit_mask.txt
blob: d40024ee56209f060cf84cb763ac9aa8911a1f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
### 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) { }
```