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 {
68 "incompatible bit mask: `_ & {}` can never be equal to `{}`",
73 } else if mask_value == 0 {
74 span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
78 if mask_value | cmp_value != cmp_value {
84 "incompatible bit mask: `_ | {}` can never be equal to `{}`",
92 BinOpKind::Lt | BinOpKind::Ge => match bit_op {
93 BinOpKind::BitAnd => {
94 if mask_value < cmp_value {
100 "incompatible bit mask: `_ & {}` will always be lower than `{}`",
101 mask_value, cmp_value
104 } else if mask_value == 0 {
105 span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
108 BinOpKind::BitOr => {
109 if mask_value >= cmp_value {
115 "incompatible bit mask: `_ | {}` will never be lower than `{}`",
116 mask_value, cmp_value
120 check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
123 BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
126 BinOpKind::Le | BinOpKind::Gt => match bit_op {
127 BinOpKind::BitAnd => {
128 if mask_value <= cmp_value {
134 "incompatible bit mask: `_ & {}` will never be higher than `{}`",
135 mask_value, cmp_value
138 } else if mask_value == 0 {
139 span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
142 BinOpKind::BitOr => {
143 if mask_value > cmp_value {
149 "incompatible bit mask: `_ | {}` will always be higher than `{}`",
150 mask_value, cmp_value
154 check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
157 BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
164 fn check_ineffective_lt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) {
165 if c.is_power_of_two() && m < c {
168 INEFFECTIVE_BIT_MASK,
171 "ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
178 fn check_ineffective_gt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) {
179 if (c + 1).is_power_of_two() && m <= c {
182 INEFFECTIVE_BIT_MASK,
185 "ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
192 fn fetch_int_literal(cx: &LateContext<'_>, lit: &Expr<'_>) -> Option<u128> {
193 match constant(cx, cx.typeck_results(), lit)?.0 {
194 Constant::Int(n) => Some(n),