Skip to content

Commit 687195c

Browse files
committed
clean-up and add comments
RE: comments: I quickly got lost in all the `op`s and `left`s and `right`s, so I wrote down them down to help me. I imagine they could be helpful to the next person that looks at this
1 parent c057abd commit 687195c

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

clippy_lints/src/operators/bit_mask.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ pub(super) fn check<'tcx>(
1515
right: &'tcx Expr<'_>,
1616
) {
1717
if op.is_comparison() {
18+
// `<left> <op> <const_right>`
1819
if let Some(cmp_opt) = fetch_int_literal(cx, right) {
1920
check_compare(cx, left, op, cmp_opt, e.span);
21+
// `<const_left> <op> <right>`
2022
} else if let Some(cmp_val) = fetch_int_literal(cx, left) {
23+
// turn into `<right> <inv(op)> <const_left>`
2124
check_compare(cx, right, invert_cmp(op), cmp_val, e.span);
2225
}
2326
}
@@ -37,13 +40,15 @@ fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
3740
}
3841

3942
fn check_compare<'a>(cx: &LateContext<'a>, bit_op: &Expr<'a>, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
40-
if let ExprKind::Binary(op, left, right) = &bit_op.kind {
41-
if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr || is_from_proc_macro(cx, bit_op) {
42-
return;
43-
}
44-
if let Some(mask) = fetch_int_literal(cx, right).or_else(|| fetch_int_literal(cx, left)) {
45-
check_bit_mask(cx, op.node, cmp_op, mask, cmp_value, span);
46-
}
43+
// whether `<bit_op> <cmp_op> <cmp_value>`
44+
// is `(<left> <op> <right>) <cmp_op> <cmp_value>`
45+
// is `(<masked> <op> <mask>) <cmp_op> <cmp_value>` (either `left` or `right` is the `mask`)
46+
if let ExprKind::Binary(op, left, right) = &bit_op.kind
47+
&& matches!(op.node, BinOpKind::BitAnd | BinOpKind::BitOr)
48+
&& !is_from_proc_macro(cx, bit_op)
49+
&& let Some(mask) = fetch_int_literal(cx, right).or_else(|| fetch_int_literal(cx, left))
50+
{
51+
check_bit_mask(cx, op.node, cmp_op, mask, cmp_value, span);
4752
}
4853
}
4954

tests/ui/bit_masks.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
#![allow(clippy::identity_op, clippy::no_effect, clippy::unnecessary_operation)]
2+
13
const THREE_BITS: i64 = 7;
24
const EVEN_MORE_REDIRECTION: i64 = THREE_BITS;
35

46
#[warn(clippy::bad_bit_mask)]
5-
#[allow(
6-
clippy::ineffective_bit_mask,
7-
clippy::identity_op,
8-
clippy::no_effect,
9-
clippy::unnecessary_operation
10-
)]
7+
#[allow(clippy::ineffective_bit_mask)]
118
fn main() {
129
let x = 5;
1310

@@ -68,7 +65,7 @@ fn main() {
6865
}
6966

7067
#[warn(clippy::ineffective_bit_mask)]
71-
#[allow(clippy::bad_bit_mask, clippy::no_effect, clippy::unnecessary_operation)]
68+
#[allow(clippy::bad_bit_mask)]
7269
fn ineffective() {
7370
let x = 5;
7471

tests/ui/bit_masks.stderr

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: &-masking with zero
2-
--> tests/ui/bit_masks.rs:14:5
2+
--> tests/ui/bit_masks.rs:11:5
33
|
44
LL | x & 0 == 0;
55
| ^^^^^^^^^^
@@ -8,81 +8,81 @@ LL | x & 0 == 0;
88
= help: to override `-D warnings` add `#[allow(clippy::bad_bit_mask)]`
99

1010
error: this operation will always return zero. This is likely not the intended outcome
11-
--> tests/ui/bit_masks.rs:14:5
11+
--> tests/ui/bit_masks.rs:11:5
1212
|
1313
LL | x & 0 == 0;
1414
| ^^^^^
1515
|
1616
= note: `#[deny(clippy::erasing_op)]` on by default
1717

1818
error: incompatible bit mask: `_ & 2` can never be equal to `1`
19-
--> tests/ui/bit_masks.rs:20:5
19+
--> tests/ui/bit_masks.rs:17:5
2020
|
2121
LL | x & 2 == 1;
2222
| ^^^^^^^^^^
2323

2424
error: incompatible bit mask: `_ | 3` can never be equal to `2`
25-
--> tests/ui/bit_masks.rs:26:5
25+
--> tests/ui/bit_masks.rs:23:5
2626
|
2727
LL | x | 3 == 2;
2828
| ^^^^^^^^^^
2929

3030
error: incompatible bit mask: `_ & 1` will never be higher than `1`
31-
--> tests/ui/bit_masks.rs:29:5
31+
--> tests/ui/bit_masks.rs:26:5
3232
|
3333
LL | x & 1 > 1;
3434
| ^^^^^^^^^
3535

3636
error: incompatible bit mask: `_ | 2` will always be higher than `1`
37-
--> tests/ui/bit_masks.rs:35:5
37+
--> tests/ui/bit_masks.rs:32:5
3838
|
3939
LL | x | 2 > 1;
4040
| ^^^^^^^^^
4141

4242
error: incompatible bit mask: `_ & 7` can never be equal to `8`
43-
--> tests/ui/bit_masks.rs:44:5
43+
--> tests/ui/bit_masks.rs:41:5
4444
|
4545
LL | x & THREE_BITS == 8;
4646
| ^^^^^^^^^^^^^^^^^^^
4747

4848
error: incompatible bit mask: `_ | 7` will never be lower than `7`
49-
--> tests/ui/bit_masks.rs:47:5
49+
--> tests/ui/bit_masks.rs:44:5
5050
|
5151
LL | x | EVEN_MORE_REDIRECTION < 7;
5252
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5353

5454
error: &-masking with zero
55-
--> tests/ui/bit_masks.rs:50:5
55+
--> tests/ui/bit_masks.rs:47:5
5656
|
5757
LL | 0 & x == 0;
5858
| ^^^^^^^^^^
5959

6060
error: this operation will always return zero. This is likely not the intended outcome
61-
--> tests/ui/bit_masks.rs:50:5
61+
--> tests/ui/bit_masks.rs:47:5
6262
|
6363
LL | 0 & x == 0;
6464
| ^^^^^
6565

6666
error: incompatible bit mask: `_ | 2` will always be higher than `1`
67-
--> tests/ui/bit_masks.rs:57:5
67+
--> tests/ui/bit_masks.rs:54:5
6868
|
6969
LL | 1 < 2 | x;
7070
| ^^^^^^^^^
7171

7272
error: incompatible bit mask: `_ | 3` can never be equal to `2`
73-
--> tests/ui/bit_masks.rs:60:5
73+
--> tests/ui/bit_masks.rs:57:5
7474
|
7575
LL | 2 == 3 | x;
7676
| ^^^^^^^^^^
7777

7878
error: incompatible bit mask: `_ & 2` can never be equal to `1`
79-
--> tests/ui/bit_masks.rs:63:5
79+
--> tests/ui/bit_masks.rs:60:5
8080
|
8181
LL | 1 == x & 2;
8282
| ^^^^^^^^^^
8383

8484
error: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared directly
85-
--> tests/ui/bit_masks.rs:75:5
85+
--> tests/ui/bit_masks.rs:72:5
8686
|
8787
LL | x | 1 > 3;
8888
| ^^^^^^^^^
@@ -91,19 +91,19 @@ LL | x | 1 > 3;
9191
= help: to override `-D warnings` add `#[allow(clippy::ineffective_bit_mask)]`
9292

9393
error: ineffective bit mask: `x | 1` compared to `4`, is the same as x compared directly
94-
--> tests/ui/bit_masks.rs:78:5
94+
--> tests/ui/bit_masks.rs:75:5
9595
|
9696
LL | x | 1 < 4;
9797
| ^^^^^^^^^
9898

9999
error: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared directly
100-
--> tests/ui/bit_masks.rs:81:5
100+
--> tests/ui/bit_masks.rs:78:5
101101
|
102102
LL | x | 1 <= 3;
103103
| ^^^^^^^^^^
104104

105105
error: ineffective bit mask: `x | 1` compared to `8`, is the same as x compared directly
106-
--> tests/ui/bit_masks.rs:84:5
106+
--> tests/ui/bit_masks.rs:81:5
107107
|
108108
LL | x | 1 >= 8;
109109
| ^^^^^^^^^^

0 commit comments

Comments
 (0)