|
| 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_and_mask_size: u8, |
| 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 | + let mut app = Applicability::MachineApplicable; |
| 28 | + let (dividend, divisor) = if lhs_op.node == BinOpKind::Rem { |
| 29 | + ( |
| 30 | + lhs_left, |
| 31 | + Sugg::hir_with_applicability(cx, lhs_right, "_", &mut app).into_string(), |
| 32 | + ) |
| 33 | + } else if lhs_op.node == BinOpKind::BitAnd { |
| 34 | + let min_divisor = 1 << u128::from(min_and_mask_size); |
| 35 | + if let Some(divisor) = is_all_ones(cx, lhs_right, min_divisor, &mut app) { |
| 36 | + (lhs_left, divisor) |
| 37 | + } else if let Some(divisor) = is_all_ones(cx, lhs_left, min_divisor, &mut app) { |
| 38 | + (lhs_right, divisor) |
| 39 | + } else { |
| 40 | + return; |
| 41 | + } |
| 42 | + } else { |
| 43 | + return; |
| 44 | + }; |
| 45 | + span_lint_and_sugg( |
| 46 | + cx, |
| 47 | + MANUAL_IS_MULTIPLE_OF, |
| 48 | + expr.span, |
| 49 | + "manual implementation of `.is_multiple_of()`", |
| 50 | + "replace with", |
| 51 | + format!( |
| 52 | + "{}{}.is_multiple_of({divisor})", |
| 53 | + if op == BinOpKind::Eq { "" } else { "!" }, |
| 54 | + Sugg::hir_with_applicability(cx, dividend, "_", &mut app).maybe_par() |
| 55 | + ), |
| 56 | + app, |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// If we have a `x == 0`, `x != 0` or `x > 0` (or the reverted ones), return the non-zero operand |
| 62 | +fn uint_compare_to_zero<'tcx>( |
| 63 | + cx: &LateContext<'tcx>, |
| 64 | + op: BinOpKind, |
| 65 | + lhs: &'tcx Expr<'tcx>, |
| 66 | + rhs: &'tcx Expr<'tcx>, |
| 67 | +) -> Option<&'tcx Expr<'tcx>> { |
| 68 | + let operand = if matches!(lhs.kind, ExprKind::Binary(..)) |
| 69 | + && matches!(op, BinOpKind::Eq | BinOpKind::Ne | BinOpKind::Gt) |
| 70 | + && is_zero_integer_const(cx, rhs) |
| 71 | + { |
| 72 | + lhs |
| 73 | + } else if matches!(rhs.kind, ExprKind::Binary(..)) |
| 74 | + && matches!(op, BinOpKind::Eq | BinOpKind::Ne | BinOpKind::Lt) |
| 75 | + && is_zero_integer_const(cx, lhs) |
| 76 | + { |
| 77 | + rhs |
| 78 | + } else { |
| 79 | + return None; |
| 80 | + }; |
| 81 | + |
| 82 | + matches!(cx.typeck_results().expr_ty_adjusted(operand).kind(), ty::Uint(_)).then_some(operand) |
| 83 | +} |
| 84 | + |
| 85 | +/// If `expr` is made of all ones, return the representation of `expr+1` if it is no smaller than |
| 86 | +/// `min_divisor`. |
| 87 | +fn is_all_ones<'tcx>( |
| 88 | + cx: &LateContext<'tcx>, |
| 89 | + expr: &'tcx Expr<'tcx>, |
| 90 | + min_divisor: u128, |
| 91 | + app: &mut Applicability, |
| 92 | +) -> Option<String> { |
| 93 | + if let ExprKind::Binary(op, lhs, rhs) = expr.kind |
| 94 | + && op.node == BinOpKind::Sub |
| 95 | + && let ExprKind::Binary(op, lhs_left, lhs_right) = lhs.kind |
| 96 | + && op.node == BinOpKind::Shl |
| 97 | + && let Some(1) = integer_const(cx, lhs_left) |
| 98 | + && let Some(1) = integer_const(cx, rhs) |
| 99 | + && integer_const(cx, lhs_right).is_none_or(|v| 1 << v >= min_divisor) |
| 100 | + { |
| 101 | + Some(Sugg::hir_with_applicability(cx, lhs, "_", app).to_string()) |
| 102 | + } else if let Some(value) = integer_const(cx, expr) |
| 103 | + && let Some(inc_value) = value.checked_add(1) |
| 104 | + && inc_value.is_power_of_two() |
| 105 | + { |
| 106 | + let repr = if expr.span.check_source_text(cx, |s| s.starts_with("0x")) { |
| 107 | + format!("{inc_value:#x}") |
| 108 | + } else { |
| 109 | + inc_value.to_string() |
| 110 | + }; |
| 111 | + (inc_value >= min_divisor).then_some(repr) |
| 112 | + } else { |
| 113 | + None |
| 114 | + } |
| 115 | +} |
0 commit comments