|
| 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::{eq_expr_value, peel_blocks}; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_session::impl_lint_pass; |
| 11 | +use rustc_span::Span; |
| 12 | +use rustc_span::source_map::Spanned; |
| 13 | + |
| 14 | +declare_clippy_lint! { |
| 15 | + /// ### What it does |
| 16 | + /// Detects patterns like `if a > b { a - b } else { b - a }` and suggests using `a.abs_diff(b)`. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// Using `abs_diff` is shorter, more readable, and avoids control flow. |
| 20 | + /// |
| 21 | + /// ### Examples |
| 22 | + /// ```no_run |
| 23 | + /// # let (a, b) = (5_usize, 3_usize); |
| 24 | + /// if a > b { |
| 25 | + /// a - b |
| 26 | + /// } else { |
| 27 | + /// b - a |
| 28 | + /// } |
| 29 | + /// # ; |
| 30 | + /// ``` |
| 31 | + /// Use instead: |
| 32 | + /// ```no_run |
| 33 | + /// # let (a, b) = (5_usize, 3_usize); |
| 34 | + /// a.abs_diff(b) |
| 35 | + /// # ; |
| 36 | + /// ``` |
| 37 | + #[clippy::version = "1.86.0"] |
| 38 | + pub MANUAL_ABS_DIFF, |
| 39 | + complexity, |
| 40 | + "using an if-else pattern instead of `abs_diff`" |
| 41 | +} |
| 42 | + |
| 43 | +impl_lint_pass!(ManualAbsDiff => [MANUAL_ABS_DIFF]); |
| 44 | + |
| 45 | +pub struct ManualAbsDiff { |
| 46 | + msrv: Msrv, |
| 47 | +} |
| 48 | + |
| 49 | +impl ManualAbsDiff { |
| 50 | + pub fn new(conf: &'static Conf) -> Self { |
| 51 | + Self { msrv: conf.msrv } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug)] |
| 56 | +struct Suggestion<'tcx> { |
| 57 | + params: Input<'tcx>, |
| 58 | + span: Span, |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug)] |
| 62 | +struct Input<'tcx> { |
| 63 | + a: &'tcx Expr<'tcx>, |
| 64 | + b: &'tcx Expr<'tcx>, |
| 65 | +} |
| 66 | + |
| 67 | +impl<'tcx> LateLintPass<'tcx> for ManualAbsDiff { |
| 68 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 69 | + if !expr.span.from_expansion() |
| 70 | + && self.msrv.meets(cx, msrvs::ABS_DIFF) |
| 71 | + && let Some(suggestion) = is_manual_abs_diff_pattern(cx, expr) |
| 72 | + { |
| 73 | + emit_suggestion(cx, &suggestion); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Checks if the given expression is a subtraction operation between two expected expressions, i.e. |
| 79 | +/// if `expr` is `{expected_a} - {expected_b}`. |
| 80 | +fn is_sub_expr(cx: &LateContext<'_>, expr: &Expr<'_>, expected_a: &Expr<'_>, expected_b: &Expr<'_>) -> bool { |
| 81 | + matches!( |
| 82 | + peel_blocks(expr).kind, |
| 83 | + ExprKind::Binary( |
| 84 | + Spanned { |
| 85 | + node: BinOpKind::Sub, |
| 86 | + .. |
| 87 | + }, |
| 88 | + a, |
| 89 | + b, |
| 90 | + ) |
| 91 | + if eq_expr_value(cx, a, expected_a) && eq_expr_value(cx, b, expected_b) |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +/// Matches a pattern such as `if a > b { a - b } else { b - a }` |
| 96 | +fn is_manual_abs_diff_pattern<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<Suggestion<'tcx>> { |
| 97 | + if let Some(If { |
| 98 | + cond, |
| 99 | + then, |
| 100 | + r#else: Some(r#else), |
| 101 | + }) = If::hir(expr) |
| 102 | + && let ExprKind::Binary( |
| 103 | + Spanned { |
| 104 | + node: BinOpKind::Gt, .. |
| 105 | + }, |
| 106 | + a, |
| 107 | + b, |
| 108 | + ) |
| 109 | + | ExprKind::Binary( |
| 110 | + Spanned { |
| 111 | + node: BinOpKind::Lt, .. |
| 112 | + }, |
| 113 | + b, |
| 114 | + a, |
| 115 | + ) = cond.kind |
| 116 | + && is_sub_expr(cx, then, a, b) |
| 117 | + && is_sub_expr(cx, r#else, b, a) |
| 118 | + { |
| 119 | + Some(Suggestion { |
| 120 | + params: Input { a, b }, |
| 121 | + span: expr.span, |
| 122 | + }) |
| 123 | + } else { |
| 124 | + None |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fn emit_suggestion<'tcx>(cx: &LateContext<'tcx>, suggestion: &Suggestion<'tcx>) { |
| 129 | + let Suggestion { |
| 130 | + params: Input { a, b, .. }, |
| 131 | + span, |
| 132 | + .. |
| 133 | + } = suggestion; |
| 134 | + |
| 135 | + let a = Sugg::hir(cx, a, "..").maybe_paren(); |
| 136 | + let b = Sugg::hir(cx, b, ".."); |
| 137 | + let suggestion = format!("{a}.abs_diff({b})"); |
| 138 | + let msg = "manual absolute difference pattern without using `abs_diff`"; |
| 139 | + let help = "replace with `abs_diff`"; |
| 140 | + span_lint_and_sugg( |
| 141 | + cx, |
| 142 | + MANUAL_ABS_DIFF, |
| 143 | + *span, |
| 144 | + msg, |
| 145 | + help, |
| 146 | + suggestion, |
| 147 | + Applicability::MachineApplicable, |
| 148 | + ); |
| 149 | +} |
0 commit comments