|
| 1 | +use clippy_utils::consts::{integer_const, is_zero_integer_const}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::msrvs::{self, Msrv}; |
| 4 | +use clippy_utils::source::SpanRangeExt; |
| 5 | +use clippy_utils::sugg::Sugg; |
| 6 | +use rustc_ast::BinOpKind; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{Expr, ExprKind}; |
| 9 | +use rustc_lint::LateContext; |
| 10 | +use rustc_middle::ty; |
| 11 | + |
| 12 | +use super::MANUAL_IS_MULTIPLE_OF; |
| 13 | + |
| 14 | +pub(super) fn check<'tcx>( |
| 15 | + cx: &LateContext<'tcx>, |
| 16 | + expr: &Expr<'_>, |
| 17 | + op: BinOpKind, |
| 18 | + lhs: &'tcx Expr<'tcx>, |
| 19 | + rhs: &'tcx Expr<'tcx>, |
| 20 | + min_divisor: u64, |
| 21 | + msrv: &Msrv, |
| 22 | +) { |
| 23 | + if let Some(operand) = uint_compare_to_zero(cx, op, lhs, rhs) |
| 24 | + && let ExprKind::Binary(lhs_op, lhs_left, lhs_right) = operand.kind |
| 25 | + && msrv.meets(msrvs::UNSIGNED_IS_MULTIPLE_OF) |
| 26 | + { |
| 27 | + const MSG: &str = "manual implementation of `.is_multiple_of()`"; |
| 28 | + let min_divisor = u128::from(min_divisor); |
| 29 | + let invert = if op == BinOpKind::Eq { "" } else { "!" }; |
| 30 | + let mut app = Applicability::MachineApplicable; |
| 31 | + |
| 32 | + if lhs_op.node == BinOpKind::Rem { |
| 33 | + if integer_const(cx, lhs_right).is_some_and(|d| d < min_divisor) { |
| 34 | + return; |
| 35 | + } |
| 36 | + span_lint_and_sugg( |
| 37 | + cx, |
| 38 | + MANUAL_IS_MULTIPLE_OF, |
| 39 | + expr.span, |
| 40 | + MSG, |
| 41 | + "replace with", |
| 42 | + format!( |
| 43 | + "{}{}.is_multiple_of({})", |
| 44 | + invert, |
| 45 | + Sugg::hir_with_applicability(cx, lhs_left, "_", &mut app).maybe_par(), |
| 46 | + Sugg::hir_with_applicability(cx, lhs_right, "_", &mut app) |
| 47 | + ), |
| 48 | + app, |
| 49 | + ); |
| 50 | + } else if lhs_op.node == BinOpKind::BitAnd { |
| 51 | + let (dividend, divisor) = if let Some(divisor) = is_all_ones(cx, lhs_right, min_divisor, &mut app) { |
| 52 | + (lhs_left, divisor) |
| 53 | + } else if let Some(divisor) = is_all_ones(cx, lhs_left, min_divisor, &mut app) { |
| 54 | + (lhs_right, divisor) |
| 55 | + } else { |
| 56 | + return; |
| 57 | + }; |
| 58 | + span_lint_and_sugg( |
| 59 | + cx, |
| 60 | + MANUAL_IS_MULTIPLE_OF, |
| 61 | + expr.span, |
| 62 | + MSG, |
| 63 | + "replace with", |
| 64 | + format!( |
| 65 | + "{}{}.is_multiple_of({divisor})", |
| 66 | + invert, |
| 67 | + Sugg::hir_with_applicability(cx, dividend, "_", &mut app).maybe_par() |
| 68 | + ), |
| 69 | + app, |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// If we have a `x == 0`, `x != 0` or `x > 0` (or the reverted ones), return the non-zero operand |
| 76 | +fn uint_compare_to_zero<'tcx>( |
| 77 | + cx: &LateContext<'tcx>, |
| 78 | + op: BinOpKind, |
| 79 | + lhs: &'tcx Expr<'tcx>, |
| 80 | + rhs: &'tcx Expr<'tcx>, |
| 81 | +) -> Option<&'tcx Expr<'tcx>> { |
| 82 | + let operand = if matches!(lhs.kind, ExprKind::Binary(..)) |
| 83 | + && matches!(op, BinOpKind::Eq | BinOpKind::Ne | BinOpKind::Gt) |
| 84 | + && is_zero_integer_const(cx, rhs) |
| 85 | + { |
| 86 | + lhs |
| 87 | + } else if matches!(rhs.kind, ExprKind::Binary(..)) |
| 88 | + && matches!(op, BinOpKind::Eq | BinOpKind::Ne | BinOpKind::Lt) |
| 89 | + && is_zero_integer_const(cx, lhs) |
| 90 | + { |
| 91 | + rhs |
| 92 | + } else { |
| 93 | + return None; |
| 94 | + }; |
| 95 | + |
| 96 | + matches!(cx.typeck_results().expr_ty_adjusted(operand).kind(), ty::Uint(_)).then_some(operand) |
| 97 | +} |
| 98 | + |
| 99 | +/// If `expr` is made of all ones, return the representation of a value `v` such as |
| 100 | +/// `1 << v == expr + 1`. If it can be determined than `1 << v` is smaller than `min_divisor`, |
| 101 | +/// return `None`. |
| 102 | +fn is_all_ones<'tcx>( |
| 103 | + cx: &LateContext<'tcx>, |
| 104 | + expr: &'tcx Expr<'tcx>, |
| 105 | + min_divisor: u128, |
| 106 | + app: &mut Applicability, |
| 107 | +) -> Option<String> { |
| 108 | + if let ExprKind::Binary(op, lhs, rhs) = expr.kind |
| 109 | + && op.node == BinOpKind::Sub |
| 110 | + && let ExprKind::Binary(op, lhs_left, lhs_right) = lhs.kind |
| 111 | + && op.node == BinOpKind::Shl |
| 112 | + && let Some(1) = integer_const(cx, lhs_left) |
| 113 | + && let Some(1) = integer_const(cx, rhs) |
| 114 | + && integer_const(cx, lhs_right).is_none_or(|v| 1 << v >= min_divisor) |
| 115 | + { |
| 116 | + Some(Sugg::hir_with_applicability(cx, lhs, "_", app).to_string()) |
| 117 | + } else if let Some(value) = integer_const(cx, expr) |
| 118 | + && let Some(inc_value) = value.checked_add(1) |
| 119 | + && inc_value.is_power_of_two() |
| 120 | + { |
| 121 | + let repr = if expr.span.check_source_text(cx, |s| s.starts_with("0x")) { |
| 122 | + format!("{inc_value:#x}") |
| 123 | + } else { |
| 124 | + inc_value.to_string() |
| 125 | + }; |
| 126 | + (inc_value >= min_divisor).then_some(repr) |
| 127 | + } else { |
| 128 | + None |
| 129 | + } |
| 130 | +} |
0 commit comments