|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use rustc_ast::LitKind; |
| 4 | +use rustc_data_structures::packed::Pu128; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{Body, Closure, Expr, ExprKind, LangItem, PatKind, QPath}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_session::declare_lint_pass; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// |
| 13 | + /// Lint to prevent trivial `map`s over ranges when one could use `take`. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// |
| 17 | + /// It expreses the intent more clearly to `take` the correct number of times |
| 18 | + /// from a generating function than to apply a closure to each number in a |
| 19 | + /// range only to discard them. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```no_run |
| 23 | + /// let random_numbers : Vec<_> = (0..10).map(|_| { 3 + 1 }).collect(); |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```no_run |
| 27 | + /// let f : Vec<_> = std::iter::repeat_with(|| { 3 + 1 }).take(10).collect(); |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.81.0"] |
| 30 | + pub TRIVIAL_MAP_OVER_RANGE, |
| 31 | + style, |
| 32 | + "map of a trivial closure (not dependent on parameter) over a range" |
| 33 | +} |
| 34 | + |
| 35 | +declare_lint_pass!(TrivialMapOverRange => [TRIVIAL_MAP_OVER_RANGE]); |
| 36 | + |
| 37 | +impl LateLintPass<'_> for TrivialMapOverRange { |
| 38 | + fn check_expr(&mut self, cx: &LateContext<'_>, ex: &Expr<'_>) { |
| 39 | + if let ExprKind::MethodCall(path, receiver, [map_arg_expr], _call_span) = ex.kind |
| 40 | + && path.ident.name == rustc_span::sym::map |
| 41 | + && let ExprKind::Struct(qpath, fields, _) = receiver.kind |
| 42 | + && matches!(qpath, QPath::LangItem(LangItem::Range, _)) |
| 43 | + && fields.len() == 2 |
| 44 | + && let ExprKind::Closure(Closure { body, .. }) = map_arg_expr.kind |
| 45 | + && let Body { params: [param], .. } = cx.tcx.hir().body(*body) |
| 46 | + && matches!(param.pat.kind, PatKind::Wild) |
| 47 | + && let ExprKind::Lit(lit) = fields[0].expr.kind |
| 48 | + && let LitKind::Int(Pu128(lower_bound), _) = lit.node |
| 49 | + { |
| 50 | + if let ExprKind::Lit(lit) = fields[1].expr.kind |
| 51 | + && let LitKind::Int(Pu128(upper_bound), _) = lit.node |
| 52 | + { |
| 53 | + let count = upper_bound - lower_bound; |
| 54 | + let mut applicability = Applicability::MaybeIncorrect; |
| 55 | + let snippet = snippet_with_applicability(cx, map_arg_expr.span, "|| { ... }", &mut applicability) |
| 56 | + .replacen("|_|", "||", 1); |
| 57 | + span_lint_and_sugg( |
| 58 | + cx, |
| 59 | + TRIVIAL_MAP_OVER_RANGE, |
| 60 | + ex.span, |
| 61 | + "map of a trivial closure (not dependent on parameter) over a range", |
| 62 | + "use", |
| 63 | + format!("std::iter::repeat_with({snippet}).take({count})"), |
| 64 | + applicability, |
| 65 | + ); |
| 66 | + } else if lower_bound == 0 { |
| 67 | + let mut applicability = Applicability::MaybeIncorrect; |
| 68 | + let count = snippet_with_applicability(cx, fields[1].expr.span, "...", &mut applicability); |
| 69 | + let snippet = snippet_with_applicability(cx, map_arg_expr.span, "|| { ... }", &mut applicability) |
| 70 | + .replacen("|_|", "||", 1); |
| 71 | + span_lint_and_sugg( |
| 72 | + cx, |
| 73 | + TRIVIAL_MAP_OVER_RANGE, |
| 74 | + ex.span, |
| 75 | + "map of a trivial closure (not dependent on parameter) over a range", |
| 76 | + "use", |
| 77 | + format!("std::iter::repeat_with({snippet}).take({count})"), |
| 78 | + applicability, |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments