|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::higher::If; |
| 4 | +use clippy_utils::msrvs::{self, Msrv}; |
| 5 | +use clippy_utils::sugg::Sugg; |
| 6 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 7 | +use clippy_utils::{eq_expr_value, peel_blocks}; |
| 8 | +use rustc_errors::Applicability; |
| 9 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 10 | +use rustc_lint::{LateContext, LateLintPass}; |
| 11 | +use rustc_middle::ty; |
| 12 | +use rustc_session::impl_lint_pass; |
| 13 | +use rustc_span::sym; |
| 14 | + |
| 15 | +declare_clippy_lint! { |
| 16 | + /// ### What it does |
| 17 | + /// Detects patterns like `if a > b { a - b } else { b - a }` and suggests using `a.abs_diff(b)`. |
| 18 | + /// |
| 19 | + /// ### Why is this bad? |
| 20 | + /// Using `abs_diff` is shorter, more readable, and avoids control flow. |
| 21 | + /// |
| 22 | + /// ### Examples |
| 23 | + /// ```no_run |
| 24 | + /// # let (a, b) = (5_usize, 3_usize); |
| 25 | + /// if a > b { |
| 26 | + /// a - b |
| 27 | + /// } else { |
| 28 | + /// b - a |
| 29 | + /// } |
| 30 | + /// # ; |
| 31 | + /// ``` |
| 32 | + /// Use instead: |
| 33 | + /// ```no_run |
| 34 | + /// # let (a, b) = (5_usize, 3_usize); |
| 35 | + /// a.abs_diff(b) |
| 36 | + /// # ; |
| 37 | + /// ``` |
| 38 | + #[clippy::version = "1.86.0"] |
| 39 | + pub MANUAL_ABS_DIFF, |
| 40 | + complexity, |
| 41 | + "using an if-else pattern instead of `abs_diff`" |
| 42 | +} |
| 43 | + |
| 44 | +impl_lint_pass!(ManualAbsDiff => [MANUAL_ABS_DIFF]); |
| 45 | + |
| 46 | +pub struct ManualAbsDiff { |
| 47 | + msrv: Msrv, |
| 48 | +} |
| 49 | + |
| 50 | +impl ManualAbsDiff { |
| 51 | + pub fn new(conf: &'static Conf) -> Self { |
| 52 | + Self { msrv: conf.msrv } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<'tcx> LateLintPass<'tcx> for ManualAbsDiff { |
| 57 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 58 | + if !expr.span.from_expansion() |
| 59 | + && let Some(if_expr) = If::hir(expr) |
| 60 | + && let Some(r#else) = if_expr.r#else |
| 61 | + && let ExprKind::Binary(op, rhs, lhs) = if_expr.cond.kind |
| 62 | + && let (BinOpKind::Gt | BinOpKind::Ge, a, b) | (BinOpKind::Lt | BinOpKind::Le, b, a) = (op.node, rhs, lhs) |
| 63 | + && self.are_ty_eligible(cx, a, b) |
| 64 | + && Self::is_sub_expr(cx, if_expr.then, a, b) |
| 65 | + && Self::is_sub_expr(cx, r#else, b, a) |
| 66 | + { |
| 67 | + let a = Sugg::hir(cx, a, "..").maybe_paren(); |
| 68 | + let b = Sugg::hir(cx, b, ".."); |
| 69 | + span_lint_and_sugg( |
| 70 | + cx, |
| 71 | + MANUAL_ABS_DIFF, |
| 72 | + expr.span, |
| 73 | + "manual absolute difference pattern without using `abs_diff`", |
| 74 | + "replace with `abs_diff`", |
| 75 | + format!("{a}.abs_diff({b})"), |
| 76 | + Applicability::MachineApplicable, |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl ManualAbsDiff { |
| 83 | + /// Returns `true` if `a` and `b` are of the same type, and this lint can be applied to that |
| 84 | + /// type (currently, any primitive unsigned int, or a `Duration`) |
| 85 | + fn are_ty_eligible(&self, cx: &LateContext<'_>, a: &Expr<'_>, b: &Expr<'_>) -> bool { |
| 86 | + let a_ty = cx.typeck_results().expr_ty(a).peel_refs(); |
| 87 | + a_ty == cx.typeck_results().expr_ty(b).peel_refs() |
| 88 | + && ((matches!(a_ty.kind(), ty::Uint(_)) && self.msrv.meets(cx, msrvs::ABS_DIFF)) |
| 89 | + || (is_type_diagnostic_item(cx, a_ty, sym::Duration) && self.msrv.meets(cx, msrvs::DURATION_ABS_DIFF))) |
| 90 | + } |
| 91 | + |
| 92 | + /// Checks if the given expression is a subtraction operation between two expected expressions, |
| 93 | + /// i.e. if `expr` is `{expected_a} - {expected_b}`. |
| 94 | + fn is_sub_expr(cx: &LateContext<'_>, expr: &Expr<'_>, expected_a: &Expr<'_>, expected_b: &Expr<'_>) -> bool { |
| 95 | + if let ExprKind::Binary(op, a, b) = peel_blocks(expr).kind |
| 96 | + && let BinOpKind::Sub = op.node |
| 97 | + && eq_expr_value(cx, a, expected_a) |
| 98 | + && eq_expr_value(cx, b, expected_b) |
| 99 | + { |
| 100 | + true |
| 101 | + } else { |
| 102 | + false |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments