1 use crate::utils::{is_type_diagnostic_item, match_def_path, paths, snippet, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_ast::ast::LitKind;
4 use rustc_errors::Applicability;
5 use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, Local, PatKind, QPath, Stmt, StmtKind};
6 use rustc_lint::{LateContext, LateLintPass, LintContext};
7 use rustc_middle::lint::in_external_macro;
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::{symbol::sym, Span, Symbol};
10 use std::convert::TryInto;
12 declare_clippy_lint! {
13 /// **What it does:** Checks for calls to `push` immediately after creating a new `Vec`.
15 /// **Why is this bad?** The `vec![]` macro is both more performant and easier to read than
16 /// multiple `push` calls.
18 /// **Known problems:** None.
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 #[derive(Clone, Copy)]
47 struct VecPushSearcher {
55 impl VecPushSearcher {
56 fn display_err(&self, cx: &LateContext<'_>) {
58 _ if self.found == 0 => return,
59 VecInitKind::WithCapacity(x) if x > self.found => return,
63 let mut s = if self.lhs_is_local {
68 s.push_str(&snippet(cx, self.lhs_span, ".."));
69 s.push_str(" = vec![..];");
75 "calls to `push` immediately after creation",
76 "consider using the `vec![]` macro",
78 Applicability::HasPlaceholders,
83 impl LateLintPass<'_> for VecInitThenPush {
84 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, _, ident, 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<'_>) {
105 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 ExprKind::Path(QPath::Resolved(_, path)) = left.kind;
110 if let Some(name) = path.segments.get(0);
111 if let Some(init_kind) = get_vec_init_kind(cx, right);
113 self.searcher = Some(VecPushSearcher {
115 name: name.ident.name,
126 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
127 if let Some(searcher) = self.searcher.take() {
129 if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind;
130 if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
131 if path.ident.name.as_str() == "push";
132 if let ExprKind::Path(QPath::Resolved(_, self_path)) = self_arg.kind;
133 if let [self_name] = self_path.segments;
134 if self_name.ident.name == searcher.name;
136 self.searcher = Some(VecPushSearcher {
137 found: searcher.found + 1,
138 err_span: searcher.err_span.to(stmt.span),
142 searcher.display_err(cx);
148 fn check_block_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
149 if let Some(searcher) = self.searcher.take() {
150 searcher.display_err(cx);
155 fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<VecInitKind> {
156 if let ExprKind::Call(func, args) = expr.kind {
158 ExprKind::Path(QPath::TypeRelative(ty, name))
159 if is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), sym::vec_type) =>
161 if name.ident.name == sym::new {
162 return Some(VecInitKind::New);
163 } else if name.ident.name.as_str() == "with_capacity" {
164 return args.get(0).and_then(|arg| {
166 if let ExprKind::Lit(lit) = &arg.kind;
167 if let LitKind::Int(num, _) = lit.node;
169 Some(VecInitKind::WithCapacity(num.try_into().ok()?))
177 ExprKind::Path(QPath::Resolved(_, path))
178 if match_def_path(cx, path.res.opt_def_id()?, &paths::DEFAULT_TRAIT_METHOD)
179 && is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::vec_type) =>
181 return Some(VecInitKind::New);