2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use clippy_utils::sugg::Sugg;
|
2021-04-22 09:31:13 +00:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
|
|
|
use clippy_utils::{get_enclosing_block, is_expr_path_def_path, path_to_local, path_to_local_id, paths, SpanlessEq};
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::LitKind;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-01-15 22:07:52 +00:00
|
|
|
use rustc_hir::intravisit::{walk_block, walk_expr, walk_stmt, Visitor};
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, PatKind, QPath, Stmt, StmtKind};
|
2021-05-06 09:51:22 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-04-22 09:31:13 +00:00
|
|
|
use rustc_span::symbol::sym;
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks slow zero-filled vector initialization
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// These structures are non-idiomatic and less efficient than simply using
|
2019-03-05 16:50:33 +00:00
|
|
|
/// `vec![0; len]`.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2019-08-03 06:01:27 +00:00
|
|
|
/// # use core::iter::repeat;
|
|
|
|
/// # let len = 4;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let mut vec1 = Vec::with_capacity(len);
|
|
|
|
/// vec1.resize(len, 0);
|
|
|
|
///
|
|
|
|
/// let mut vec2 = Vec::with_capacity(len);
|
2020-06-09 14:36:01 +00:00
|
|
|
/// vec2.extend(repeat(0).take(len));
|
2022-06-16 15:39:06 +00:00
|
|
|
/// ```
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
2022-06-16 15:39:06 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// # let len = 4;
|
2020-06-09 14:36:01 +00:00
|
|
|
/// let mut vec1 = vec![0; len];
|
|
|
|
/// let mut vec2 = vec![0; len];
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.32.0"]
|
2018-10-18 23:15:48 +00:00
|
|
|
pub SLOW_VECTOR_INITIALIZATION,
|
|
|
|
perf,
|
2018-10-29 23:25:05 +00:00
|
|
|
"slow vector initialization"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(SlowVectorInit => [SLOW_VECTOR_INITIALIZATION]);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
/// `VecAllocation` contains data regarding a vector allocated with `with_capacity` and then
|
2018-10-18 23:15:48 +00:00
|
|
|
/// assigned to a variable. For example, `let mut vec = Vec::with_capacity(0)` or
|
|
|
|
/// `vec = Vec::with_capacity(0)`
|
2018-10-30 20:47:15 +00:00
|
|
|
struct VecAllocation<'tcx> {
|
2021-04-22 09:31:13 +00:00
|
|
|
/// HirId of the variable
|
|
|
|
local_id: HirId,
|
2018-10-18 23:15:48 +00:00
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
/// Reference to the expression which allocates the vector
|
2019-12-27 07:12:26 +00:00
|
|
|
allocation_expr: &'tcx Expr<'tcx>,
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
/// Reference to the expression used as argument on `with_capacity` call. This is used
|
|
|
|
/// to only match slow zero-filling idioms of the same length than vector initialization.
|
2019-12-27 07:12:26 +00:00
|
|
|
len_expr: &'tcx Expr<'tcx>,
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Type of slow initialization
|
|
|
|
enum InitializationType<'tcx> {
|
|
|
|
/// Extend is a slow initialization with the form `vec.extend(repeat(0).take(..))`
|
2019-12-27 07:12:26 +00:00
|
|
|
Extend(&'tcx Expr<'tcx>),
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
/// Resize is a slow initialization with the form `vec.resize(.., 0)`
|
2019-12-27 07:12:26 +00:00
|
|
|
Resize(&'tcx Expr<'tcx>),
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for SlowVectorInit {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2018-10-18 23:15:48 +00:00
|
|
|
// Matches initialization on reassignements. For example: `vec = Vec::with_capacity(100)`
|
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let ExprKind::Assign(left, right, _) = expr.kind;
|
2018-10-18 23:15:48 +00:00
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
// Extract variable
|
|
|
|
if let Some(local_id) = path_to_local(left);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
// Extract len argument
|
2021-04-22 09:31:13 +00:00
|
|
|
if let Some(len_arg) = Self::is_vec_with_capacity(cx, right);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
then {
|
2018-10-30 20:47:15 +00:00
|
|
|
let vi = VecAllocation {
|
2021-04-22 09:31:13 +00:00
|
|
|
local_id,
|
2018-10-30 20:47:15 +00:00
|
|
|
allocation_expr: right,
|
2018-10-18 23:15:48 +00:00
|
|
|
len_expr: len_arg,
|
|
|
|
};
|
|
|
|
|
2019-02-24 18:43:15 +00:00
|
|
|
Self::search_initialization(cx, vi, expr.hir_id);
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
|
2018-10-18 23:15:48 +00:00
|
|
|
// Matches statements which initializes vectors. For example: `let mut vec = Vec::with_capacity(10)`
|
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let StmtKind::Local(local) = stmt.kind;
|
2021-04-22 09:31:13 +00:00
|
|
|
if let PatKind::Binding(BindingAnnotation::Mutable, local_id, _, None) = local.pat.kind;
|
2021-04-08 15:50:13 +00:00
|
|
|
if let Some(init) = local.init;
|
2021-04-22 09:31:13 +00:00
|
|
|
if let Some(len_arg) = Self::is_vec_with_capacity(cx, init);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
then {
|
2018-10-30 20:47:15 +00:00
|
|
|
let vi = VecAllocation {
|
2021-04-22 09:31:13 +00:00
|
|
|
local_id,
|
2018-10-30 20:47:15 +00:00
|
|
|
allocation_expr: init,
|
2018-10-18 23:15:48 +00:00
|
|
|
len_expr: len_arg,
|
|
|
|
};
|
|
|
|
|
2019-02-24 18:43:15 +00:00
|
|
|
Self::search_initialization(cx, vi, stmt.hir_id);
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl SlowVectorInit {
|
2018-10-18 23:15:48 +00:00
|
|
|
/// Checks if the given expression is `Vec::with_capacity(..)`. It will return the expression
|
|
|
|
/// of the first argument of `with_capacity` call if it matches or `None` if it does not.
|
2021-04-22 09:31:13 +00:00
|
|
|
fn is_vec_with_capacity<'tcx>(cx: &LateContext<'_>, expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
|
2018-10-18 23:15:48 +00:00
|
|
|
if_chain! {
|
2021-04-22 09:31:13 +00:00
|
|
|
if let ExprKind::Call(func, [arg]) = expr.kind;
|
|
|
|
if let ExprKind::Path(QPath::TypeRelative(ty, name)) = func.kind;
|
|
|
|
if name.ident.as_str() == "with_capacity";
|
2021-10-02 23:51:01 +00:00
|
|
|
if is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), sym::Vec);
|
2018-10-18 23:15:48 +00:00
|
|
|
then {
|
2021-04-22 09:31:13 +00:00
|
|
|
Some(arg)
|
|
|
|
} else {
|
|
|
|
None
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
/// Search initialization for the given vector
|
2020-06-25 20:41:36 +00:00
|
|
|
fn search_initialization<'tcx>(cx: &LateContext<'tcx>, vec_alloc: VecAllocation<'tcx>, parent_node: HirId) {
|
2018-10-18 23:15:48 +00:00
|
|
|
let enclosing_body = get_enclosing_block(cx, parent_node);
|
|
|
|
|
|
|
|
if enclosing_body.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
let mut v = VectorInitializationVisitor {
|
2018-10-18 23:15:48 +00:00
|
|
|
cx,
|
2018-10-30 20:47:15 +00:00
|
|
|
vec_alloc,
|
2018-10-18 23:15:48 +00:00
|
|
|
slow_expression: None,
|
|
|
|
initialization_found: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
v.visit_block(enclosing_body.unwrap());
|
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
if let Some(ref allocation_expr) = v.slow_expression {
|
2018-11-05 21:46:07 +00:00
|
|
|
Self::lint_initialization(cx, allocation_expr, &v.vec_alloc);
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-29 23:25:05 +00:00
|
|
|
|
2018-11-27 20:14:15 +00:00
|
|
|
fn lint_initialization<'tcx>(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'tcx>,
|
2018-11-27 20:14:15 +00:00
|
|
|
initialization: &InitializationType<'tcx>,
|
|
|
|
vec_alloc: &VecAllocation<'_>,
|
|
|
|
) {
|
2018-10-29 23:25:05 +00:00
|
|
|
match initialization {
|
2021-05-06 09:51:22 +00:00
|
|
|
InitializationType::Extend(e) | InitializationType::Resize(e) => {
|
2021-06-03 06:41:37 +00:00
|
|
|
Self::emit_lint(cx, e, vec_alloc, "slow zero-filling initialization");
|
2021-05-06 09:51:22 +00:00
|
|
|
},
|
2018-10-29 23:25:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:51:22 +00:00
|
|
|
fn emit_lint<'tcx>(cx: &LateContext<'tcx>, slow_fill: &Expr<'_>, vec_alloc: &VecAllocation<'_>, msg: &str) {
|
2018-10-30 20:47:15 +00:00
|
|
|
let len_expr = Sugg::hir(cx, vec_alloc.len_expr, "len");
|
2018-10-29 23:25:05 +00:00
|
|
|
|
2021-05-06 09:51:22 +00:00
|
|
|
span_lint_and_then(cx, SLOW_VECTOR_INITIALIZATION, slow_fill.span, msg, |diag| {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2018-11-27 20:14:15 +00:00
|
|
|
vec_alloc.allocation_expr.span,
|
|
|
|
"consider replace allocation with",
|
|
|
|
format!("vec![0; {}]", len_expr),
|
|
|
|
Applicability::Unspecified,
|
|
|
|
);
|
|
|
|
});
|
2018-10-29 23:25:05 +00:00
|
|
|
}
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
/// `VectorInitializationVisitor` searches for unsafe or slow vector initializations for the given
|
2018-10-18 23:15:48 +00:00
|
|
|
/// vector.
|
2019-06-19 18:36:23 +00:00
|
|
|
struct VectorInitializationVisitor<'a, 'tcx> {
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2018-10-18 23:15:48 +00:00
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Contains the information.
|
2018-10-30 20:47:15 +00:00
|
|
|
vec_alloc: VecAllocation<'tcx>,
|
2018-10-18 23:15:48 +00:00
|
|
|
|
2019-03-10 17:19:47 +00:00
|
|
|
/// Contains the slow initialization expression, if one was found.
|
2018-10-18 23:15:48 +00:00
|
|
|
slow_expression: Option<InitializationType<'tcx>>,
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// `true` if the initialization of the vector has been found on the visited block.
|
2018-10-18 23:15:48 +00:00
|
|
|
initialization_found: bool,
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
|
2018-10-18 23:15:48 +00:00
|
|
|
/// Checks if the given expression is extending a vector with `repeat(0).take(..)`
|
2019-12-27 07:12:26 +00:00
|
|
|
fn search_slow_extend_filling(&mut self, expr: &'tcx Expr<'_>) {
|
2018-10-18 23:15:48 +00:00
|
|
|
if_chain! {
|
|
|
|
if self.initialization_found;
|
2021-12-01 17:17:50 +00:00
|
|
|
if let ExprKind::MethodCall(path, [self_arg, extend_arg], _) = expr.kind;
|
2021-04-22 09:31:13 +00:00
|
|
|
if path_to_local_id(self_arg, self.vec_alloc.local_id);
|
2019-05-17 21:53:54 +00:00
|
|
|
if path.ident.name == sym!(extend);
|
2018-10-18 23:15:48 +00:00
|
|
|
if self.is_repeat_take(extend_arg);
|
|
|
|
|
|
|
|
then {
|
|
|
|
self.slow_expression = Some(InitializationType::Extend(expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the given expression is resizing a vector with 0
|
2019-12-27 07:12:26 +00:00
|
|
|
fn search_slow_resize_filling(&mut self, expr: &'tcx Expr<'_>) {
|
2018-10-18 23:15:48 +00:00
|
|
|
if_chain! {
|
|
|
|
if self.initialization_found;
|
2021-12-01 17:17:50 +00:00
|
|
|
if let ExprKind::MethodCall(path, [self_arg, len_arg, fill_arg], _) = expr.kind;
|
2021-04-22 09:31:13 +00:00
|
|
|
if path_to_local_id(self_arg, self.vec_alloc.local_id);
|
2019-05-17 21:53:54 +00:00
|
|
|
if path.ident.name == sym!(resize);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
// Check that is filled with 0
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Lit(ref lit) = fill_arg.kind;
|
2018-10-18 23:15:48 +00:00
|
|
|
if let LitKind::Int(0, _) = lit.node;
|
|
|
|
|
|
|
|
// Check that len expression is equals to `with_capacity` expression
|
2018-10-30 20:47:15 +00:00
|
|
|
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
then {
|
|
|
|
self.slow_expression = Some(InitializationType::Resize(expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if give expression is `repeat(0).take(...)`
|
2019-12-27 07:12:26 +00:00
|
|
|
fn is_repeat_take(&self, expr: &Expr<'_>) -> bool {
|
2018-10-18 23:15:48 +00:00
|
|
|
if_chain! {
|
2021-12-01 17:17:50 +00:00
|
|
|
if let ExprKind::MethodCall(take_path, take_args, _) = expr.kind;
|
2019-05-17 21:53:54 +00:00
|
|
|
if take_path.ident.name == sym!(take);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
// Check that take is applied to `repeat(0)`
|
2021-04-08 15:50:13 +00:00
|
|
|
if let Some(repeat_expr) = take_args.get(0);
|
2021-04-22 09:31:13 +00:00
|
|
|
if self.is_repeat_zero(repeat_expr);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
// Check that len expression is equals to `with_capacity` expression
|
2021-04-08 15:50:13 +00:00
|
|
|
if let Some(len_arg) = take_args.get(1);
|
2018-10-30 20:47:15 +00:00
|
|
|
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
|
2018-10-18 23:15:48 +00:00
|
|
|
|
|
|
|
then {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if given expression is `repeat(0)`
|
2021-04-22 09:31:13 +00:00
|
|
|
fn is_repeat_zero(&self, expr: &Expr<'_>) -> bool {
|
2018-10-18 23:15:48 +00:00
|
|
|
if_chain! {
|
2021-04-22 09:31:13 +00:00
|
|
|
if let ExprKind::Call(fn_expr, [repeat_arg]) = expr.kind;
|
|
|
|
if is_expr_path_def_path(self.cx, fn_expr, &paths::ITER_REPEAT);
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Lit(ref lit) = repeat_arg.kind;
|
2018-10-18 23:15:48 +00:00
|
|
|
if let LitKind::Int(0, _) = lit.node;
|
|
|
|
|
|
|
|
then {
|
2021-04-22 09:31:13 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:47:15 +00:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for VectorInitializationVisitor<'a, 'tcx> {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
|
2018-11-18 22:45:53 +00:00
|
|
|
if self.initialization_found {
|
2019-09-27 15:16:06 +00:00
|
|
|
match stmt.kind {
|
2021-04-08 15:50:13 +00:00
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
|
2018-11-18 22:45:53 +00:00
|
|
|
self.search_slow_extend_filling(expr);
|
|
|
|
self.search_slow_resize_filling(expr);
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
self.initialization_found = false;
|
|
|
|
} else {
|
|
|
|
walk_stmt(self, stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_block(&mut self, block: &'tcx Block<'_>) {
|
2018-11-18 22:45:53 +00:00
|
|
|
if self.initialization_found {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let Some(s) = block.stmts.get(0) {
|
2021-06-03 06:41:37 +00:00
|
|
|
self.visit_stmt(s);
|
2018-11-18 22:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.initialization_found = false;
|
|
|
|
} else {
|
|
|
|
walk_block(self, block);
|
2018-10-18 23:15:48 +00:00
|
|
|
}
|
2018-11-18 22:45:53 +00:00
|
|
|
}
|
2018-10-18 23:15:48 +00:00
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
2018-10-18 23:15:48 +00:00
|
|
|
// Skip all the expressions previous to the vector initialization
|
2019-02-24 18:43:15 +00:00
|
|
|
if self.vec_alloc.allocation_expr.hir_id == expr.hir_id {
|
2018-10-18 23:15:48 +00:00
|
|
|
self.initialization_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|