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::{path_to_local, path_to_local_id};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, 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};
13 declare_clippy_lint! {
15 /// Checks for calls to `push` immediately after creating a new `Vec`.
17 /// ### Why is this bad?
18 /// The `vec![]` macro is both more performant and easier to read than
19 /// multiple `push` calls.
23 /// let mut v = Vec::new();
30 #[clippy::version = "1.51.0"]
31 pub VEC_INIT_THEN_PUSH,
33 "`push` immediately after `Vec` creation"
36 impl_lint_pass!(VecInitThenPush => [VEC_INIT_THEN_PUSH]);
39 pub struct VecInitThenPush {
40 searcher: Option<VecPushSearcher>,
43 struct VecPushSearcher {
51 impl VecPushSearcher {
52 fn display_err(&self, cx: &LateContext<'_>) {
54 _ if self.found == 0 => return,
55 VecInitKind::WithLiteralCapacity(x) if x > self.found => return,
56 VecInitKind::WithExprCapacity(_) => return,
60 let mut s = if self.lhs_is_local {
65 s.push_str(&snippet(cx, self.lhs_span, ".."));
66 s.push_str(" = vec![..];");
72 "calls to `push` immediately after creation",
73 "consider using the `vec![]` macro",
75 Applicability::HasPlaceholders,
80 impl LateLintPass<'_> for VecInitThenPush {
81 fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
85 fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
87 if !in_external_macro(cx.sess(), local.span);
88 if let Some(init) = local.init;
89 if let PatKind::Binding(BindingAnnotation::Mutable, id, _, None) = local.pat.kind;
90 if let Some(init_kind) = get_vec_init_kind(cx, init);
92 self.searcher = Some(VecPushSearcher {
96 lhs_span: local.ty.map_or(local.pat.span, |t| local.pat.span.to(t.span)),
104 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
106 if self.searcher.is_none();
107 if !in_external_macro(cx.sess(), expr.span);
108 if let ExprKind::Assign(left, right, _) = expr.kind;
109 if let Some(id) = path_to_local(left);
110 if let Some(init_kind) = get_vec_init_kind(cx, right);
112 self.searcher = Some(VecPushSearcher {
124 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
125 if let Some(searcher) = self.searcher.take() {
127 if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind;
128 if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
129 if path_to_local_id(self_arg, searcher.local_id);
130 if path.ident.name.as_str() == "push";
132 self.searcher = Some(VecPushSearcher {
133 found: searcher.found + 1,
134 err_span: searcher.err_span.to(stmt.span),
138 searcher.display_err(cx);
144 fn check_block_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
145 if let Some(searcher) = self.searcher.take() {
146 searcher.display_err(cx);