|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::higher::{get_vec_init_kind, VecInitKind}; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use clippy_utils::{is_from_proc_macro, path_to_local_id}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::def::Res; |
| 7 | +use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, QPath, Stmt, StmtKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 11 | +use rustc_span::Span; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Informs the user about a more concise way to create a vector with a known capacity. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// The `Vec::with_capacity` constructor is less complex. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust |
| 22 | + /// let mut v: Vec<usize> = vec![]; |
| 23 | + /// v.reserve(10); |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// let mut v: Vec<usize> = Vec::with_capacity(10); |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.73.0"] |
| 30 | + pub RESERVE_AFTER_INITIALIZATION, |
| 31 | + complexity, |
| 32 | + "`reserve` called immediatly after `Vec` creation" |
| 33 | +} |
| 34 | +impl_lint_pass!(ReserveAfterInitialization => [RESERVE_AFTER_INITIALIZATION]); |
| 35 | + |
| 36 | +#[derive(Default)] |
| 37 | +pub struct ReserveAfterInitialization { |
| 38 | + searcher: Option<VecReserveSearcher>, |
| 39 | +} |
| 40 | + |
| 41 | +struct VecReserveSearcher { |
| 42 | + local_id: HirId, |
| 43 | + err_span: Span, |
| 44 | + init_part: String, |
| 45 | + space_hint: String, |
| 46 | +} |
| 47 | +impl VecReserveSearcher { |
| 48 | + fn display_err(&self, cx: &LateContext<'_>) { |
| 49 | + if self.space_hint.is_empty() { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + let s = format!("{}Vec::with_capacity({});", self.init_part, self.space_hint); |
| 54 | + |
| 55 | + span_lint_and_sugg( |
| 56 | + cx, |
| 57 | + RESERVE_AFTER_INITIALIZATION, |
| 58 | + self.err_span, |
| 59 | + "call to `reserve` immediately after creation", |
| 60 | + "consider using `Vec::with_capacity(/* Space hint */)`", |
| 61 | + s, |
| 62 | + Applicability::HasPlaceholders, |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization { |
| 68 | + fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) { |
| 69 | + self.searcher = None; |
| 70 | + } |
| 71 | + |
| 72 | + fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) { |
| 73 | + if let Some(init_expr) = local.init |
| 74 | + && let PatKind::Binding(BindingAnnotation::MUT, id, _, None) = local.pat.kind |
| 75 | + && !in_external_macro(cx.sess(), local.span) |
| 76 | + && let Some(init) = get_vec_init_kind(cx, init_expr) |
| 77 | + && !matches!(init, VecInitKind::WithExprCapacity(_) | VecInitKind::WithConstCapacity(_)) |
| 78 | + { |
| 79 | + self.searcher = Some(VecReserveSearcher { |
| 80 | + local_id: id, |
| 81 | + err_span: local.span, |
| 82 | + init_part: snippet(cx, local.span.shrink_to_lo() |
| 83 | + .to(init_expr.span.source_callsite().shrink_to_lo()), "..") |
| 84 | + .into_owned(), |
| 85 | + space_hint: String::new() |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 91 | + if self.searcher.is_none() |
| 92 | + && let ExprKind::Assign(left, right, _) = expr.kind |
| 93 | + && let ExprKind::Path(QPath::Resolved(None, path)) = left.kind |
| 94 | + && let Res::Local(id) = path.res |
| 95 | + && !in_external_macro(cx.sess(), expr.span) |
| 96 | + && let Some(init) = get_vec_init_kind(cx, right) |
| 97 | + && !matches!(init, VecInitKind::WithExprCapacity(_) | VecInitKind::WithConstCapacity(_)) |
| 98 | + { |
| 99 | + self.searcher = Some(VecReserveSearcher { |
| 100 | + local_id: id, |
| 101 | + err_span: expr.span, |
| 102 | + init_part: snippet(cx, left.span.shrink_to_lo() |
| 103 | + .to(right.span.source_callsite().shrink_to_lo()), "..") |
| 104 | + .into_owned(), // see `assign_expression` test |
| 105 | + space_hint: String::new() |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) { |
| 111 | + if let Some(searcher) = self.searcher.take() { |
| 112 | + if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind |
| 113 | + && let ExprKind::MethodCall(name, self_arg, [space_hint], _) = expr.kind |
| 114 | + && path_to_local_id(self_arg, searcher.local_id) |
| 115 | + && name.ident.as_str() == "reserve" |
| 116 | + && !is_from_proc_macro(cx, expr) |
| 117 | + { |
| 118 | + self.searcher = Some(VecReserveSearcher { |
| 119 | + err_span: searcher.err_span.to(stmt.span), |
| 120 | + space_hint: snippet(cx, space_hint.span, "..").into_owned(), |
| 121 | + .. searcher |
| 122 | + }); |
| 123 | + } else { |
| 124 | + searcher.display_err(cx); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + fn check_block_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Block<'tcx>) { |
| 130 | + if let Some(searcher) = self.searcher.take() { |
| 131 | + searcher.display_err(cx); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments