Skip to content

Commit 2941ccf

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 2941ccf

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
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

0 commit comments

Comments
 (0)