2021-02-05 22:27:23 +00:00
|
|
|
use crate::utils::{
|
|
|
|
is_type_diagnostic_item, match_def_path, path_to_local, path_to_local_id, paths, snippet, span_lint_and_sugg,
|
|
|
|
};
|
2021-01-02 16:08:56 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast::LitKind;
|
|
|
|
use rustc_errors::Applicability;
|
2021-02-05 22:27:23 +00:00
|
|
|
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, QPath, Stmt, StmtKind};
|
2021-01-02 16:08:56 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-02-05 22:27:23 +00:00
|
|
|
use rustc_span::{symbol::sym, Span};
|
2021-01-02 19:31:21 +00:00
|
|
|
use std::convert::TryInto;
|
2021-01-02 16:08:56 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for calls to `push` immediately after creating a new `Vec`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The `vec![]` macro is both more performant and easier to read than
|
|
|
|
/// multiple `push` calls.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
2021-01-03 19:04:05 +00:00
|
|
|
/// let mut v = Vec::new();
|
2021-01-02 16:08:56 +00:00
|
|
|
/// v.push(0);
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2021-01-03 19:04:05 +00:00
|
|
|
/// let v = vec![0];
|
2021-01-02 16:08:56 +00:00
|
|
|
/// ```
|
|
|
|
pub VEC_INIT_THEN_PUSH,
|
|
|
|
perf,
|
|
|
|
"`push` immediately after `Vec` creation"
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(VecInitThenPush => [VEC_INIT_THEN_PUSH]);
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct VecInitThenPush {
|
|
|
|
searcher: Option<VecPushSearcher>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum VecInitKind {
|
|
|
|
New,
|
|
|
|
WithCapacity(u64),
|
|
|
|
}
|
|
|
|
struct VecPushSearcher {
|
2021-02-05 22:27:23 +00:00
|
|
|
local_id: HirId,
|
2021-01-02 16:08:56 +00:00
|
|
|
init: VecInitKind,
|
|
|
|
lhs_is_local: bool,
|
|
|
|
lhs_span: Span,
|
|
|
|
err_span: Span,
|
|
|
|
found: u64,
|
|
|
|
}
|
|
|
|
impl VecPushSearcher {
|
|
|
|
fn display_err(&self, cx: &LateContext<'_>) {
|
|
|
|
match self.init {
|
|
|
|
_ if self.found == 0 => return,
|
|
|
|
VecInitKind::WithCapacity(x) if x > self.found => return,
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut s = if self.lhs_is_local {
|
|
|
|
String::from("let ")
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
s.push_str(&snippet(cx, self.lhs_span, ".."));
|
|
|
|
s.push_str(" = vec![..];");
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
VEC_INIT_THEN_PUSH,
|
|
|
|
self.err_span,
|
|
|
|
"calls to `push` immediately after creation",
|
|
|
|
"consider using the `vec![]` macro",
|
|
|
|
s,
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for VecInitThenPush {
|
2021-02-05 22:28:13 +00:00
|
|
|
fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
|
2021-01-02 16:08:56 +00:00
|
|
|
self.searcher = None;
|
2021-02-05 22:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
|
2021-01-02 16:08:56 +00:00
|
|
|
if_chain! {
|
|
|
|
if !in_external_macro(cx.sess(), local.span);
|
|
|
|
if let Some(init) = local.init;
|
2021-02-05 22:27:23 +00:00
|
|
|
if let PatKind::Binding(BindingAnnotation::Mutable, id, _, None) = local.pat.kind;
|
2021-01-02 16:08:56 +00:00
|
|
|
if let Some(init_kind) = get_vec_init_kind(cx, init);
|
|
|
|
then {
|
|
|
|
self.searcher = Some(VecPushSearcher {
|
2021-02-05 22:27:23 +00:00
|
|
|
local_id: id,
|
2021-01-02 16:08:56 +00:00
|
|
|
init: init_kind,
|
|
|
|
lhs_is_local: true,
|
2021-01-02 19:31:21 +00:00
|
|
|
lhs_span: local.ty.map_or(local.pat.span, |t| local.pat.span.to(t.span)),
|
2021-01-02 16:08:56 +00:00
|
|
|
err_span: local.span,
|
|
|
|
found: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|
|
|
if self.searcher.is_none() {
|
|
|
|
if_chain! {
|
|
|
|
if !in_external_macro(cx.sess(), expr.span);
|
|
|
|
if let ExprKind::Assign(left, right, _) = expr.kind;
|
2021-02-05 22:27:23 +00:00
|
|
|
if let Some(id) = path_to_local(left);
|
2021-01-02 16:08:56 +00:00
|
|
|
if let Some(init_kind) = get_vec_init_kind(cx, right);
|
|
|
|
then {
|
|
|
|
self.searcher = Some(VecPushSearcher {
|
2021-02-05 22:27:23 +00:00
|
|
|
local_id: id,
|
2021-01-02 16:08:56 +00:00
|
|
|
init: init_kind,
|
|
|
|
lhs_is_local: false,
|
|
|
|
lhs_span: left.span,
|
|
|
|
err_span: expr.span,
|
|
|
|
found: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
|
|
|
|
if let Some(searcher) = self.searcher.take() {
|
|
|
|
if_chain! {
|
|
|
|
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind;
|
|
|
|
if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
|
2021-02-05 22:27:23 +00:00
|
|
|
if path_to_local_id(self_arg, searcher.local_id);
|
2021-01-02 16:08:56 +00:00
|
|
|
if path.ident.name.as_str() == "push";
|
|
|
|
then {
|
|
|
|
self.searcher = Some(VecPushSearcher {
|
|
|
|
found: searcher.found + 1,
|
|
|
|
err_span: searcher.err_span.to(stmt.span),
|
|
|
|
.. searcher
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
searcher.display_err(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_block_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
|
|
|
|
if let Some(searcher) = self.searcher.take() {
|
|
|
|
searcher.display_err(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<VecInitKind> {
|
|
|
|
if let ExprKind::Call(func, args) = expr.kind {
|
|
|
|
match func.kind {
|
|
|
|
ExprKind::Path(QPath::TypeRelative(ty, name))
|
|
|
|
if is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), sym::vec_type) =>
|
|
|
|
{
|
2020-12-29 22:04:31 +00:00
|
|
|
if name.ident.name == sym::new {
|
2021-01-02 16:08:56 +00:00
|
|
|
return Some(VecInitKind::New);
|
|
|
|
} else if name.ident.name.as_str() == "with_capacity" {
|
|
|
|
return args.get(0).and_then(|arg| {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Lit(lit) = &arg.kind;
|
|
|
|
if let LitKind::Int(num, _) = lit.node;
|
|
|
|
then {
|
2021-01-02 19:31:21 +00:00
|
|
|
Some(VecInitKind::WithCapacity(num.try_into().ok()?))
|
2021-01-02 16:08:56 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExprKind::Path(QPath::Resolved(_, path))
|
|
|
|
if match_def_path(cx, path.res.opt_def_id()?, &paths::DEFAULT_TRAIT_METHOD)
|
|
|
|
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::vec_type) =>
|
|
|
|
{
|
|
|
|
return Some(VecInitKind::New);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|