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