|
| 1 | +use clippy_utils::consts::{constant_full_int, FullInt}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::source::snippet_with_applicability; |
| 4 | +use clippy_utils::{meets_msrv, msrvs, path_to_local}; |
| 5 | +use if_chain::if_chain; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_semver::RustcVersion; |
| 10 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for an expression like `((x % 4) + 4) % 4` which is a common manual reimplementation |
| 15 | + /// of `x.rem_euclid(4)`. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// It's simpler and more readable. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust |
| 22 | + /// let x = 24; |
| 23 | + /// let rem = ((x % 4) + 4) % 4; |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// let x = 24; |
| 28 | + /// let rem = x.rem_euclid(4); |
| 29 | + /// ``` |
| 30 | + #[clippy::version = "1.63.0"] |
| 31 | + pub MANUAL_REM_EUCLID, |
| 32 | + complexity, |
| 33 | + "manually reimplementing `rem_euclid`" |
| 34 | +} |
| 35 | + |
| 36 | +pub struct ManualRemEuclid { |
| 37 | + msrv: Option<RustcVersion>, |
| 38 | +} |
| 39 | + |
| 40 | +impl ManualRemEuclid { |
| 41 | + #[must_use] |
| 42 | + pub fn new(msrv: Option<RustcVersion>) -> Self { |
| 43 | + Self { msrv } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl_lint_pass!(ManualRemEuclid => [MANUAL_REM_EUCLID]); |
| 48 | + |
| 49 | +impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid { |
| 50 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 51 | + if !meets_msrv(self.msrv, msrvs::REM_EUCLID) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if_chain! { |
| 56 | + if let ExprKind::Binary(op1, ..) = expr.kind; |
| 57 | + if op1.node == BinOpKind::Rem; |
| 58 | + if let Some((const1, expr1)) = check_for_positive_int_constant(cx, expr); |
| 59 | + if let ExprKind::Binary(op2, ..) = expr1.kind; |
| 60 | + if op2.node == BinOpKind::Add; |
| 61 | + if let Some((const2, expr2)) = check_for_positive_int_constant(cx, expr1); |
| 62 | + if let ExprKind::Binary(op3, ..) = expr2.kind; |
| 63 | + if op3.node == BinOpKind::Rem; |
| 64 | + if let Some((const3, expr3)) = check_for_positive_int_constant(cx, expr2); |
| 65 | + if const1 == const2 && const2 == const3; |
| 66 | + if path_to_local(expr3).is_some(); |
| 67 | + then { |
| 68 | + let mut app = Applicability::MachineApplicable; |
| 69 | + let rem_of = snippet_with_applicability(cx, expr3.span, "_", &mut app); |
| 70 | + span_lint_and_sugg( |
| 71 | + cx, |
| 72 | + MANUAL_REM_EUCLID, |
| 73 | + expr.span, |
| 74 | + "manual `rem_euclid` implementation", |
| 75 | + "consider using", |
| 76 | + format!("{rem_of}.rem_euclid({const1})"), |
| 77 | + app, |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + extract_msrv_attr!(LateContext); |
| 84 | +} |
| 85 | + |
| 86 | +// Takes a binary expression and separates the operands into the int constant and the other |
| 87 | +// operand. Ensures the int constant is positive. |
| 88 | +fn check_for_positive_int_constant<'a>(cx: &'a LateContext<'_>, expr: &'a Expr<'_>) -> Option<(u128, &'a Expr<'a>)> { |
| 89 | + let (int_const, other_op) = if let ExprKind::Binary(_, left, right) = expr.kind { |
| 90 | + if let Some(int_const) = constant_full_int(cx, cx.typeck_results(), left) { |
| 91 | + (int_const, right) |
| 92 | + } else if let Some(int_const) = constant_full_int(cx, cx.typeck_results(), right) { |
| 93 | + (int_const, left) |
| 94 | + } else { |
| 95 | + return None; |
| 96 | + } |
| 97 | + } else { |
| 98 | + return None; |
| 99 | + }; |
| 100 | + |
| 101 | + if int_const > FullInt::S(0) { |
| 102 | + let val = match int_const { |
| 103 | + FullInt::S(s) => s.try_into().unwrap(), |
| 104 | + FullInt::U(u) => u, |
| 105 | + }; |
| 106 | + Some((val, other_op)) |
| 107 | + } else { |
| 108 | + None |
| 109 | + } |
| 110 | +} |
0 commit comments