Do not lint inside macros
This commit is contained in:
parent
df8bb47f17
commit
7977d209b2
@ -1,7 +1,7 @@
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::higher::{get_vec_init_kind, VecInitKind};
|
||||
use clippy_utils::path_to_local_id;
|
||||
use clippy_utils::source::snippet;
|
||||
use clippy_utils::{is_from_proc_macro, path_to_local_id};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, QPath, Stmt, StmtKind};
|
||||
@ -74,8 +74,9 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
|
||||
&& let PatKind::Binding(BindingAnnotation::MUT, id, _, None) = local.pat.kind
|
||||
&& !in_external_macro(cx.sess(), local.span)
|
||||
&& let Some(init) = get_vec_init_kind(cx, init_expr)
|
||||
&& !matches!(init, VecInitKind::WithExprCapacity(_))
|
||||
&& !matches!(init, VecInitKind::WithConstCapacity(_))
|
||||
&& !matches!(init, VecInitKind::WithExprCapacity(_)
|
||||
| VecInitKind::WithConstCapacity(_)
|
||||
)
|
||||
{
|
||||
self.searcher = Some(VecReserveSearcher {
|
||||
local_id: id,
|
||||
@ -116,6 +117,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
|
||||
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind
|
||||
&& let ExprKind::MethodCall(name, self_arg, [space_hint], _) = expr.kind
|
||||
&& path_to_local_id(self_arg, searcher.local_id)
|
||||
&& !is_from_proc_macro(cx, expr)
|
||||
&& name.ident.as_str() == "reserve"
|
||||
{
|
||||
self.searcher = Some(VecReserveSearcher {
|
||||
|
@ -1,6 +1,10 @@
|
||||
//@aux-build:proc_macros.rs
|
||||
#![warn(clippy::reserve_after_initialization)]
|
||||
#![no_main]
|
||||
|
||||
extern crate proc_macros;
|
||||
use proc_macros::{external, with_span};
|
||||
|
||||
// Should lint
|
||||
fn standard() {
|
||||
let mut v1: Vec<usize> = Vec::with_capacity(10);
|
||||
@ -29,28 +33,16 @@ fn assign_expression() {
|
||||
v5 = Vec::with_capacity(10);
|
||||
}
|
||||
|
||||
/*fn in_macros() {
|
||||
fn in_macros() {
|
||||
external! {
|
||||
// Should lint
|
||||
let mut v1: Vec<usize> = vec![];
|
||||
v1.reserve(10);
|
||||
|
||||
// Should lint
|
||||
let capacity = 10;
|
||||
let mut v2: Vec<usize> = vec![];
|
||||
v2.reserve(capacity);
|
||||
let mut v: Vec<usize> = vec![];
|
||||
v.reserve(10);
|
||||
}
|
||||
|
||||
with_span! {
|
||||
span
|
||||
|
||||
// Should lint
|
||||
let mut v1: Vec<usize> = vec![];
|
||||
v1.reserve(10);
|
||||
|
||||
// Should lint
|
||||
let capacity = 10;
|
||||
let mut v2: Vec<usize> = vec![];
|
||||
v2.reserve(capacity);
|
||||
let mut v: Vec<usize> = vec![];
|
||||
v.reserve(10);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
//@aux-build:proc_macros.rs
|
||||
#![warn(clippy::reserve_after_initialization)]
|
||||
#![no_main]
|
||||
|
||||
extern crate proc_macros;
|
||||
use proc_macros::{external, with_span};
|
||||
|
||||
// Should lint
|
||||
fn standard() {
|
||||
let mut v1: Vec<usize> = vec![];
|
||||
@ -32,28 +36,16 @@ fn assign_expression() {
|
||||
v5.reserve(10);
|
||||
}
|
||||
|
||||
/*fn in_macros() {
|
||||
fn in_macros() {
|
||||
external! {
|
||||
// Should lint
|
||||
let mut v1: Vec<usize> = vec![];
|
||||
v1.reserve(10);
|
||||
|
||||
// Should lint
|
||||
let capacity = 10;
|
||||
let mut v2: Vec<usize> = vec![];
|
||||
v2.reserve(capacity);
|
||||
let mut v: Vec<usize> = vec![];
|
||||
v.reserve(10);
|
||||
}
|
||||
|
||||
with_span! {
|
||||
span
|
||||
|
||||
// Should lint
|
||||
let mut v1: Vec<usize> = vec![];
|
||||
v1.reserve(10);
|
||||
|
||||
// Should lint
|
||||
let capacity = 10;
|
||||
let mut v2: Vec<usize> = vec![];
|
||||
v2.reserve(capacity);
|
||||
let mut v: Vec<usize> = vec![];
|
||||
v.reserve(10);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: call to `reserve` immediately after creation
|
||||
--> $DIR/reserve_after_initialization.rs:6:5
|
||||
--> $DIR/reserve_after_initialization.rs:10:5
|
||||
|
|
||||
LL | / let mut v1: Vec<usize> = vec![];
|
||||
LL | | v1.reserve(10);
|
||||
@ -8,14 +8,14 @@ LL | | v1.reserve(10);
|
||||
= note: `-D clippy::reserve-after-initialization` implied by `-D warnings`
|
||||
|
||||
error: call to `reserve` immediately after creation
|
||||
--> $DIR/reserve_after_initialization.rs:13:5
|
||||
--> $DIR/reserve_after_initialization.rs:17:5
|
||||
|
|
||||
LL | / let mut v2: Vec<usize> = vec![];
|
||||
LL | | v2.reserve(capacity);
|
||||
| |_________________________^ help: consider using `Vec::with_capacity(/* Space hint */)`: `let mut v2: Vec<usize> = Vec::with_capacity(capacity);`
|
||||
|
||||
error: call to `reserve` immediately after creation
|
||||
--> $DIR/reserve_after_initialization.rs:31:5
|
||||
--> $DIR/reserve_after_initialization.rs:35:5
|
||||
|
|
||||
LL | / v5 = Vec::new();
|
||||
LL | | v5.reserve(10);
|
||||
|
Loading…
x
Reference in New Issue
Block a user