| 
 | 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 msrv.meets(cx, msrvs::UNSIGNED_IS_MULTIPLE_OF)  | 
 | 24 | +        && let Some(operand) = uint_compare_to_zero(cx, op, lhs, rhs)  | 
 | 25 | +        && let ExprKind::Binary(lhs_op, lhs_left, lhs_right) = operand.kind  | 
 | 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 = 1u128 << 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_paren()  | 
 | 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 provably made of all ones, return the representation of `expr+1` if it is no  | 
 | 86 | +/// smaller than `min_divisor`. This will catch expressions of the following forms:  | 
 | 87 | +/// - `(1 << A) - 1` where `A` is a constant  | 
 | 88 | +/// - integer literals — if it uses hexadecimal, the return value will as well  | 
 | 89 | +///  | 
 | 90 | +/// The function will not attempt to evaluate non-literal constant expressions, as those may depend  | 
 | 91 | +/// on conditional compilation.  | 
 | 92 | +fn is_all_ones<'tcx>(  | 
 | 93 | +    cx: &LateContext<'tcx>,  | 
 | 94 | +    expr: &'tcx Expr<'tcx>,  | 
 | 95 | +    min_divisor: u128,  | 
 | 96 | +    app: &mut Applicability,  | 
 | 97 | +) -> Option<String> {  | 
 | 98 | +    if let ExprKind::Binary(op, lhs, rhs) = expr.kind  | 
 | 99 | +        && op.node == BinOpKind::Sub  | 
 | 100 | +        && let ExprKind::Binary(op, lhs_left, lhs_right) = lhs.kind  | 
 | 101 | +        && op.node == BinOpKind::Shl  | 
 | 102 | +        && let Some(1) = integer_const(cx, lhs_left)  | 
 | 103 | +        && let Some(1) = integer_const(cx, rhs)  | 
 | 104 | +        && integer_const(cx, lhs_right).is_none_or(|v| 1 << v >= min_divisor)  | 
 | 105 | +    {  | 
 | 106 | +        Some(Sugg::hir_with_applicability(cx, lhs, "_", app).to_string())  | 
 | 107 | +    } else if let Some(value) = integer_const(cx, expr)  | 
 | 108 | +        && let Some(inc_value) = value.checked_add(1)  | 
 | 109 | +        && inc_value.is_power_of_two()  | 
 | 110 | +    {  | 
 | 111 | +        let repr = if expr.span.check_source_text(cx, |s| s.starts_with("0x")) {  | 
 | 112 | +            format!("{inc_value:#x}")  | 
 | 113 | +        } else {  | 
 | 114 | +            inc_value.to_string()  | 
 | 115 | +        };  | 
 | 116 | +        (inc_value >= min_divisor).then_some(repr)  | 
 | 117 | +    } else {  | 
 | 118 | +        None  | 
 | 119 | +    }  | 
 | 120 | +}  | 
0 commit comments