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 pub VEC_INIT_THEN_PUSH,
32 "`push` immediately after `Vec` creation"
35 impl_lint_pass!(VecInitThenPush => [VEC_INIT_THEN_PUSH]);
38 pub struct VecInitThenPush {
39 searcher: Option<VecPushSearcher>,
42 struct VecPushSearcher {
50 impl VecPushSearcher {
51 fn display_err(&self, cx: &LateContext<'_>) {
53 _ if self.found == 0 => return,
54 VecInitKind::WithLiteralCapacity(x) if x > self.found => return,
55 VecInitKind::WithExprCapacity(_) => return,
59 let mut s = if self.lhs_is_local {
64 s.push_str(&snippet(cx, self.lhs_span, ".."));
65 s.push_str(" = vec![..];");
71 "calls to `push` immediately after creation",
72 "consider using the `vec![]` macro",
74 Applicability::HasPlaceholders,
79 impl LateLintPass<'_> for VecInitThenPush {
80 fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
84 fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
86 if !in_external_macro(cx.sess(), local.span);
87 if let Some(init) = local.init;
88 if let PatKind::Binding(BindingAnnotation::Mutable, id, _, None) = local.pat.kind;
89 if let Some(init_kind) = get_vec_init_kind(cx, init);
91 self.searcher = Some(VecPushSearcher {
95 lhs_span: local.ty.map_or(local.pat.span, |t| local.pat.span.to(t.span)),
103 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
105 if self.searcher.is_none();
106 if !in_external_macro(cx.sess(), expr.span);
107 if let ExprKind::Assign(left, right, _) = expr.kind;
108 if let Some(id) = path_to_local(left);
109 if let Some(init_kind) = get_vec_init_kind(cx, right);
111 self.searcher = Some(VecPushSearcher {
123 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
124 if let Some(searcher) = self.searcher.take() {
126 if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind;
127 if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
128 if path_to_local_id(self_arg, searcher.local_id);
129 if path.ident.name.as_str() == "push";
131 self.searcher = Some(VecPushSearcher {
132 found: searcher.found + 1,
133 err_span: searcher.err_span.to(stmt.span),
137 searcher.display_err(cx);
143 fn check_block_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
144 if let Some(searcher) = self.searcher.take() {
145 searcher.display_err(cx);