Skip to content

Commit 7977d20

Browse files
committed
Do not lint inside macros
1 parent df8bb47 commit 7977d20

File tree

4 files changed

+28
-42
lines changed

4 files changed

+28
-42
lines changed

clippy_lints/src/reserve_after_initialization.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::higher::{get_vec_init_kind, VecInitKind};
3-
use clippy_utils::path_to_local_id;
43
use clippy_utils::source::snippet;
4+
use clippy_utils::{is_from_proc_macro, path_to_local_id};
55
use rustc_errors::Applicability;
66
use rustc_hir::def::Res;
77
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, QPath, Stmt, StmtKind};
@@ -74,8 +74,9 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
7474
&& let PatKind::Binding(BindingAnnotation::MUT, id, _, None) = local.pat.kind
7575
&& !in_external_macro(cx.sess(), local.span)
7676
&& let Some(init) = get_vec_init_kind(cx, init_expr)
77-
&& !matches!(init, VecInitKind::WithExprCapacity(_))
78-
&& !matches!(init, VecInitKind::WithConstCapacity(_))
77+
&& !matches!(init, VecInitKind::WithExprCapacity(_)
78+
| VecInitKind::WithConstCapacity(_)
79+
)
7980
{
8081
self.searcher = Some(VecReserveSearcher {
8182
local_id: id,
@@ -116,6 +117,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
116117
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind
117118
&& let ExprKind::MethodCall(name, self_arg, [space_hint], _) = expr.kind
118119
&& path_to_local_id(self_arg, searcher.local_id)
120+
&& !is_from_proc_macro(cx, expr)
119121
&& name.ident.as_str() == "reserve"
120122
{
121123
self.searcher = Some(VecReserveSearcher {

tests/ui/reserve_after_initialization.fixed

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::reserve_after_initialization)]
23
#![no_main]
34

5+
extern crate proc_macros;
6+
use proc_macros::{external, with_span};
7+
48
// Should lint
59
fn standard() {
610
let mut v1: Vec<usize> = Vec::with_capacity(10);
@@ -29,28 +33,16 @@ fn assign_expression() {
2933
v5 = Vec::with_capacity(10);
3034
}
3135

32-
/*fn in_macros() {
36+
fn in_macros() {
3337
external! {
34-
// Should lint
35-
let mut v1: Vec<usize> = vec![];
36-
v1.reserve(10);
37-
38-
// Should lint
39-
let capacity = 10;
40-
let mut v2: Vec<usize> = vec![];
41-
v2.reserve(capacity);
38+
let mut v: Vec<usize> = vec![];
39+
v.reserve(10);
4240
}
4341

4442
with_span! {
4543
span
4644

47-
// Should lint
48-
let mut v1: Vec<usize> = vec![];
49-
v1.reserve(10);
50-
51-
// Should lint
52-
let capacity = 10;
53-
let mut v2: Vec<usize> = vec![];
54-
v2.reserve(capacity);
45+
let mut v: Vec<usize> = vec![];
46+
v.reserve(10);
5547
}
56-
}*/
48+
}

tests/ui/reserve_after_initialization.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::reserve_after_initialization)]
23
#![no_main]
34

5+
extern crate proc_macros;
6+
use proc_macros::{external, with_span};
7+
48
// Should lint
59
fn standard() {
610
let mut v1: Vec<usize> = vec![];
@@ -32,28 +36,16 @@ fn assign_expression() {
3236
v5.reserve(10);
3337
}
3438

35-
/*fn in_macros() {
39+
fn in_macros() {
3640
external! {
37-
// Should lint
38-
let mut v1: Vec<usize> = vec![];
39-
v1.reserve(10);
40-
41-
// Should lint
42-
let capacity = 10;
43-
let mut v2: Vec<usize> = vec![];
44-
v2.reserve(capacity);
41+
let mut v: Vec<usize> = vec![];
42+
v.reserve(10);
4543
}
4644

4745
with_span! {
4846
span
4947

50-
// Should lint
51-
let mut v1: Vec<usize> = vec![];
52-
v1.reserve(10);
53-
54-
// Should lint
55-
let capacity = 10;
56-
let mut v2: Vec<usize> = vec![];
57-
v2.reserve(capacity);
48+
let mut v: Vec<usize> = vec![];
49+
v.reserve(10);
5850
}
59-
}*/
51+
}

tests/ui/reserve_after_initialization.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: call to `reserve` immediately after creation
2-
--> $DIR/reserve_after_initialization.rs:6:5
2+
--> $DIR/reserve_after_initialization.rs:10:5
33
|
44
LL | / let mut v1: Vec<usize> = vec![];
55
LL | | v1.reserve(10);
@@ -8,14 +8,14 @@ LL | | v1.reserve(10);
88
= note: `-D clippy::reserve-after-initialization` implied by `-D warnings`
99

1010
error: call to `reserve` immediately after creation
11-
--> $DIR/reserve_after_initialization.rs:13:5
11+
--> $DIR/reserve_after_initialization.rs:17:5
1212
|
1313
LL | / let mut v2: Vec<usize> = vec![];
1414
LL | | v2.reserve(capacity);
1515
| |_________________________^ help: consider using `Vec::with_capacity(/* Space hint */)`: `let mut v2: Vec<usize> = Vec::with_capacity(capacity);`
1616

1717
error: call to `reserve` immediately after creation
18-
--> $DIR/reserve_after_initialization.rs:31:5
18+
--> $DIR/reserve_after_initialization.rs:35:5
1919
|
2020
LL | / v5 = Vec::new();
2121
LL | | v5.reserve(10);

0 commit comments

Comments
 (0)