Skip to content

Commit 3f4e599

Browse files
committed
Auto merge of rust-lang#11094 - y21:issue11084, r=Alexendoo
[`useless_vec`]: add more tests and don't lint inside of macros Closes rust-lang#11084. I realized that the fix I added in rust-lang#11081 itself also causes an error in a suggestion when inside of a macro. Example: ```rs macro_rules! x { () => { for _ in vec![1, 2] {} } } x!(); ``` Here it would suggest replacing `vec![1, 2]` with `[x!()]`, because that's what the source callsite is (reminder: it does this to get the correct span of `x!()` for code like `for _ in vec![x!()]`), but that's wrong when *inside* macros, so I decided to make it not lint if the whole loop construct is inside a macro to avoid this issue. changelog: [`useless_vec`]: add more tests and don't lint inside of macros r? `@Alexendoo` since these were your tests, I figured it makes most sense to assign you
2 parents ba3bd8f + b4549c5 commit 3f4e599

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

clippy_lints/src/vec.rs

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ impl UselessVec {
154154
span: Span,
155155
suggest_slice: SuggestedType,
156156
) {
157+
if span.from_expansion() {
158+
return;
159+
}
160+
157161
let mut applicability = Applicability::MachineApplicable;
158162

159163
let snippet = match *vec_args {

tests/ui/vec.fixed

+29
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,35 @@ fn issue11075() {
124124
for _string in [repro!(true), repro!(null)] {
125125
unimplemented!();
126126
}
127+
128+
macro_rules! in_macro {
129+
($e:expr, $vec:expr, $vec2:expr) => {{
130+
vec![1; 2].fill(3);
131+
vec![1, 2].fill(3);
132+
for _ in vec![1, 2] {}
133+
for _ in vec![1; 2] {}
134+
for _ in vec![$e, $e] {}
135+
for _ in vec![$e; 2] {}
136+
for _ in $vec {}
137+
for _ in $vec2 {}
138+
}};
139+
}
140+
141+
in_macro!(1, [1, 2], [1; 2]);
142+
143+
macro_rules! from_macro {
144+
() => {
145+
vec![1, 2, 3]
146+
};
147+
}
148+
macro_rules! from_macro_repeat {
149+
() => {
150+
vec![1; 3]
151+
};
152+
}
153+
154+
for _ in from_macro!() {}
155+
for _ in from_macro_repeat!() {}
127156
}
128157

129158
#[clippy::msrv = "1.53"]

tests/ui/vec.rs

+29
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,35 @@ fn issue11075() {
124124
for _string in vec![repro!(true), repro!(null)] {
125125
unimplemented!();
126126
}
127+
128+
macro_rules! in_macro {
129+
($e:expr, $vec:expr, $vec2:expr) => {{
130+
vec![1; 2].fill(3);
131+
vec![1, 2].fill(3);
132+
for _ in vec![1, 2] {}
133+
for _ in vec![1; 2] {}
134+
for _ in vec![$e, $e] {}
135+
for _ in vec![$e; 2] {}
136+
for _ in $vec {}
137+
for _ in $vec2 {}
138+
}};
139+
}
140+
141+
in_macro!(1, vec![1, 2], vec![1; 2]);
142+
143+
macro_rules! from_macro {
144+
() => {
145+
vec![1, 2, 3]
146+
};
147+
}
148+
macro_rules! from_macro_repeat {
149+
() => {
150+
vec![1; 3]
151+
};
152+
}
153+
154+
for _ in from_macro!() {}
155+
for _ in from_macro_repeat!() {}
127156
}
128157

129158
#[clippy::msrv = "1.53"]

tests/ui/vec.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,28 @@ LL | for _string in vec![repro!(true), repro!(null)] {
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[repro!(true), repro!(null)]`
9292

9393
error: useless use of `vec!`
94-
--> $DIR/vec.rs:131:14
94+
--> $DIR/vec.rs:141:18
95+
|
96+
LL | in_macro!(1, vec![1, 2], vec![1; 2]);
97+
| ^^^^^^^^^^ help: you can use an array directly: `[1, 2]`
98+
99+
error: useless use of `vec!`
100+
--> $DIR/vec.rs:141:30
101+
|
102+
LL | in_macro!(1, vec![1, 2], vec![1; 2]);
103+
| ^^^^^^^^^^ help: you can use an array directly: `[1; 2]`
104+
105+
error: useless use of `vec!`
106+
--> $DIR/vec.rs:160:14
95107
|
96108
LL | for a in vec![1, 2, 3] {
97109
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
98110

99111
error: useless use of `vec!`
100-
--> $DIR/vec.rs:135:14
112+
--> $DIR/vec.rs:164:14
101113
|
102114
LL | for a in vec![String::new(), String::new()] {
103115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[String::new(), String::new()]`
104116

105-
error: aborting due to 17 previous errors
117+
error: aborting due to 19 previous errors
106118

0 commit comments

Comments
 (0)