1 use clippy_utils::consts::{constant, Constant};
2 use clippy_utils::diagnostics::span_lint;
3 use rustc_hir::{BinOpKind, Expr, ExprKind};
4 use rustc_lint::LateContext;
5 use rustc_span::source_map::Span;
7 use super::{BAD_BIT_MASK, INEFFECTIVE_BIT_MASK};
9 pub(super) fn check<'tcx>(
10 cx: &LateContext<'tcx>,
14 right: &'tcx Expr<'_>,
16 if op.is_comparison() {
17 if let Some(cmp_opt) = fetch_int_literal(cx, right) {
18 check_compare(cx, left, op, cmp_opt, e.span);
19 } else if let Some(cmp_val) = fetch_int_literal(cx, left) {
20 check_compare(cx, right, invert_cmp(op), cmp_val, e.span);
26 fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
28 BinOpKind::Eq => BinOpKind::Eq,
29 BinOpKind::Ne => BinOpKind::Ne,
30 BinOpKind::Lt => BinOpKind::Gt,
31 BinOpKind::Gt => BinOpKind::Lt,
32 BinOpKind::Le => BinOpKind::Ge,
33 BinOpKind::Ge => BinOpKind::Le,
34 _ => BinOpKind::Or, // Dummy
38 fn check_compare(cx: &LateContext<'_>, bit_op: &Expr<'_>, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
39 if let ExprKind::Binary(op, left, right) = &bit_op.kind {
40 if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
43 fetch_int_literal(cx, right)
44 .or_else(|| fetch_int_literal(cx, left))
45 .map_or((), |mask| check_bit_mask(cx, op.node, cmp_op, mask, cmp_value, span));
49 #[allow(clippy::too_many_lines)]
59 BinOpKind::Eq | BinOpKind::Ne => match bit_op {
60 BinOpKind::BitAnd => {
61 if mask_value & cmp_value != cmp_value {
67 &format!("incompatible bit mask: `_ & {mask_value}` can never be equal to `{cmp_value}`"),
70 } else if mask_value == 0 {
71 span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
75 if mask_value | cmp_value != cmp_value {
80 &format!("incompatible bit mask: `_ | {mask_value}` can never be equal to `{cmp_value}`"),
86 BinOpKind::Lt | BinOpKind::Ge => match bit_op {
87 BinOpKind::BitAnd => {
88 if mask_value < cmp_value {
93 &format!("incompatible bit mask: `_ & {mask_value}` will always be lower than `{cmp_value}`"),
95 } else if mask_value == 0 {
96 span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
100 if mask_value >= cmp_value {
105 &format!("incompatible bit mask: `_ | {mask_value}` will never be lower than `{cmp_value}`"),
108 check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
111 BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
114 BinOpKind::Le | BinOpKind::Gt => match bit_op {
115 BinOpKind::BitAnd => {
116 if mask_value <= cmp_value {
121 &format!("incompatible bit mask: `_ & {mask_value}` will never be higher than `{cmp_value}`"),
123 } else if mask_value == 0 {
124 span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
127 BinOpKind::BitOr => {
128 if mask_value > cmp_value {
133 &format!("incompatible bit mask: `_ | {mask_value}` will always be higher than `{cmp_value}`"),
136 check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
139 BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
146 fn check_ineffective_lt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) {
147 if c.is_power_of_two() && m < c {
150 INEFFECTIVE_BIT_MASK,
152 &format!("ineffective bit mask: `x {op} {m}` compared to `{c}`, is the same as x compared directly"),
157 fn check_ineffective_gt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) {
158 if (c + 1).is_power_of_two() && m <= c {
161 INEFFECTIVE_BIT_MASK,
163 &format!("ineffective bit mask: `x {op} {m}` compared to `{c}`, is the same as x compared directly"),
168 fn fetch_int_literal(cx: &LateContext<'_>, lit: &Expr<'_>) -> Option<u128> {
169 match constant(cx, cx.typeck_results(), lit)?.0 {
170 Constant::Int(n) => Some(n),