|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use clippy_utils::sym; |
| 4 | +use clippy_utils::ty::{get_iterator_item_ty, has_non_owning_mutable_access, implements_trait}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::def_id::LocalDefId; |
| 7 | +use rustc_hir::intravisit::FnKind; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_middle::ty::{self}; |
| 10 | +use rustc_span::Span; |
| 11 | + |
| 12 | +use rustc_hir::*; |
| 13 | +use rustc_lint::LateLintPass; |
| 14 | +use rustc_session::impl_lint_pass; |
| 15 | + |
| 16 | +declare_clippy_lint! { |
| 17 | + /// ### What it does |
| 18 | + /// Checks for functions returning `Vec<T>` where the `T` is produced from a `_.collect::<Vec<T>>()` |
| 19 | + /// |
| 20 | + /// ### Why is this bad? |
| 21 | + /// |
| 22 | + /// This might not be necessary, and its more idiomatic to simply return the iterator, giving the caller the option to use it as they see fit. |
| 23 | + /// Its often silly to have the equivalent of `iterator.collect::<Vec<_>>().into_iter()` in your code. |
| 24 | + /// |
| 25 | + /// ### Potential Issues |
| 26 | + /// |
| 27 | + /// In some cases, there may be some lifetimes attached to your iterator that make it difficult, or even impossible, to avoid a collection. |
| 28 | + /// |
| 29 | + /// ### Example |
| 30 | + /// ```no_run |
| 31 | + /// fn squares() -> Vec<(u8, u8)> { |
| 32 | + /// (0..8u8) |
| 33 | + /// .flat_map(|x| (0..8).map(move |y| (x, y))) |
| 34 | + /// .collect::<Vec<_>>() |
| 35 | + /// } |
| 36 | + /// ``` |
| 37 | + /// Use instead: |
| 38 | + /// ```no_run |
| 39 | + /// fn squares() -> impl Iterator<Item = (u8, u8)> { |
| 40 | + /// (0..8u8) |
| 41 | + /// .flat_map(|x| (0..8).map(move |y| (x, y))) |
| 42 | + /// .collect::<Vec<_>>() |
| 43 | + /// } |
| 44 | + /// ``` |
| 45 | + #[clippy::version = "1.92.0"] |
| 46 | + pub UNNECESSARY_COLLECT, |
| 47 | + pedantic, |
| 48 | + "default lint description" |
| 49 | +} |
| 50 | + |
| 51 | +pub struct UnnecessaryCollect { |
| 52 | + avoid_breaking_exported_api: bool, |
| 53 | +} |
| 54 | + |
| 55 | +impl UnnecessaryCollect { |
| 56 | + pub fn new(conf: &'static Conf) -> Self { |
| 57 | + Self { |
| 58 | + avoid_breaking_exported_api: conf.avoid_breaking_exported_api, |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl_lint_pass!(UnnecessaryCollect => [UNNECESSARY_COLLECT]); |
| 64 | + |
| 65 | +impl<'tcx> LateLintPass<'tcx> for UnnecessaryCollect { |
| 66 | + fn check_fn( |
| 67 | + &mut self, |
| 68 | + cx: &LateContext<'tcx>, |
| 69 | + kind: FnKind<'tcx>, |
| 70 | + decl: &'tcx FnDecl<'tcx>, |
| 71 | + body: &'tcx Body<'tcx>, |
| 72 | + _: Span, |
| 73 | + def_id: LocalDefId, |
| 74 | + ) { |
| 75 | + let expr = match kind { |
| 76 | + FnKind::ItemFn(..) | FnKind::Method(..) if let ExprKind::Block(block, _) = &body.value.kind => { |
| 77 | + if let Some(block_expr) = block.expr { |
| 78 | + block_expr |
| 79 | + } else if let Some(Stmt { |
| 80 | + kind: StmtKind::Expr(expr), |
| 81 | + .. |
| 82 | + }) = block.stmts.last() |
| 83 | + { |
| 84 | + expr |
| 85 | + } else { |
| 86 | + return; |
| 87 | + } |
| 88 | + }, |
| 89 | + _ => return, |
| 90 | + } |
| 91 | + .peel_drop_temps(); |
| 92 | + if let ExprKind::MethodCall(path, iter_expr, args, call_span) = expr.kind |
| 93 | + && !(self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id)) |
| 94 | + && !args.iter().any(|e| e.span.from_expansion()) |
| 95 | + && !iter_expr.span.from_expansion() |
| 96 | + && path.ident.name == sym::collect |
| 97 | + && let iter_ty = cx.typeck_results().expr_ty(iter_expr) |
| 98 | + && !has_non_owning_mutable_access(cx, iter_ty) |
| 99 | + && let Some(iterator_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator) |
| 100 | + && implements_trait(cx, iter_ty, iterator_trait_id, &[]) |
| 101 | + && let Some(item) = get_iterator_item_ty(cx, iter_ty) |
| 102 | + && let vec_ty = cx.typeck_results().expr_ty(expr) |
| 103 | + && let ty::Adt(v, args) = vec_ty.kind() |
| 104 | + && cx.tcx.is_diagnostic_item(sym::Vec, v.did()) |
| 105 | + // make sure we're collecting to just a vec, and not a vec<result> String or other such tomfoolery |
| 106 | + && args.get(0).and_then(|x| x.as_type()) == Some(item) |
| 107 | + { |
| 108 | + span_lint_and_then( |
| 109 | + cx, |
| 110 | + UNNECESSARY_COLLECT, |
| 111 | + call_span, |
| 112 | + "this collection may not be necessary. consider returning the iterator itself".to_string(), |
| 113 | + |diag| { |
| 114 | + diag.span_label(call_span, "remove this collect"); |
| 115 | + diag.span_suggestion( |
| 116 | + decl.output.span(), |
| 117 | + "perhaps", |
| 118 | + format!("impl Iterator<Item = {item}>"), |
| 119 | + Applicability::MaybeIncorrect, |
| 120 | + ); |
| 121 | + }, |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments