Skip to content

Commit e2c655b

Browse files
committed
Auto merge of #10909 - Alexendoo:useless-for-in-vec, r=llogiq
Fix `useless_vec` suggestion in `for _ in vec![..]` Fixes rust-lang/rust#111034 Fixes #2256 changelog: [`useless_vec`]: Fix suggestion in `for _ in vec![..]`
2 parents b7c330f + 96697d2 commit e2c655b

File tree

5 files changed

+88
-51
lines changed

5 files changed

+88
-51
lines changed

clippy_lints/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
699699
});
700700
let too_large_for_stack = conf.too_large_for_stack;
701701
store.register_late_pass(move |_| Box::new(escape::BoxedLocal { too_large_for_stack }));
702-
store.register_late_pass(move |_| Box::new(vec::UselessVec { too_large_for_stack }));
702+
store.register_late_pass(move |_| {
703+
Box::new(vec::UselessVec {
704+
too_large_for_stack,
705+
msrv: msrv(),
706+
})
707+
});
703708
store.register_late_pass(|_| Box::new(panic_unimplemented::PanicUnimplemented));
704709
store.register_late_pass(|_| Box::new(strings::StringLitAsBytes));
705710
store.register_late_pass(|_| Box::new(derive::Derive));

clippy_lints/src/vec.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ops::ControlFlow;
22

33
use clippy_utils::consts::{constant, Constant};
44
use clippy_utils::diagnostics::span_lint_and_sugg;
5+
use clippy_utils::msrvs::{self, Msrv};
56
use clippy_utils::source::snippet_with_applicability;
67
use clippy_utils::ty::is_copy;
78
use clippy_utils::visitors::for_each_local_use_after_expr;
@@ -18,9 +19,10 @@ use rustc_span::source_map::Span;
1819
use rustc_span::sym;
1920

2021
#[expect(clippy::module_name_repetitions)]
21-
#[derive(Copy, Clone)]
22+
#[derive(Clone)]
2223
pub struct UselessVec {
2324
pub too_large_for_stack: u64,
25+
pub msrv: Msrv,
2426
}
2527

2628
declare_clippy_lint! {
@@ -122,14 +124,16 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
122124
if_chain! {
123125
if let Some(higher::ForLoop { arg, .. }) = higher::ForLoop::hir(expr);
124126
if let Some(vec_args) = higher::VecArgs::hir(cx, arg);
125-
if is_copy(cx, vec_type(cx.typeck_results().expr_ty_adjusted(arg)));
127+
if self.msrv.meets(msrvs::ARRAY_INTO_ITERATOR);
126128
then {
127129
// report the error around the `vec!` not inside `<std macros>:`
128130
let span = arg.span.ctxt().outer_expn_data().call_site;
129-
self.check_vec_macro(cx, &vec_args, Mutability::Not, span, SuggestSlice::Yes);
131+
self.check_vec_macro(cx, &vec_args, Mutability::Not, span, SuggestSlice::No);
130132
}
131133
}
132134
}
135+
136+
extract_msrv_attr!(LateContext);
133137
}
134138

135139
#[derive(Copy, Clone)]
@@ -142,7 +146,7 @@ enum SuggestSlice {
142146

143147
impl UselessVec {
144148
fn check_vec_macro<'tcx>(
145-
self,
149+
&mut self,
146150
cx: &LateContext<'tcx>,
147151
vec_args: &higher::VecArgs<'tcx>,
148152
mutability: Mutability,

tests/ui/vec.fixed

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
//@run-rustfix
22
#![warn(clippy::useless_vec)]
3-
#![allow(clippy::nonstandard_macro_braces, clippy::uninlined_format_args)]
3+
#![allow(clippy::nonstandard_macro_braces, clippy::uninlined_format_args, unused)]
44

55
use std::rc::Rc;
66

77
struct StructWithVec {
88
_x: Vec<i32>,
99
}
1010

11-
#[derive(Debug)]
12-
struct NonCopy;
13-
1411
fn on_slice(_: &[u8]) {}
1512

1613
fn on_mut_slice(_: &mut [u8]) {}
@@ -66,14 +63,6 @@ fn main() {
6663
on_mut_slice(&mut vec![2; line.length]);
6764
on_mut_slice(&mut vec![2; line.length()]);
6865

69-
for a in &[1, 2, 3] {
70-
println!("{:?}", a);
71-
}
72-
73-
for a in vec![NonCopy, NonCopy] {
74-
println!("{:?}", a);
75-
}
76-
7766
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7867
on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7968

@@ -91,7 +80,7 @@ fn main() {
9180

9281
let _x: &[i32] = &[1, 2, 3];
9382

94-
for _ in &[1, 2, 3] {}
83+
for _ in [1, 2, 3] {}
9584

9685
// Don't lint
9786
let x = vec![1, 2, 3];
@@ -122,3 +111,25 @@ fn main() {
122111
// Too large
123112
let _x = vec![1; 201];
124113
}
114+
115+
#[clippy::msrv = "1.53"]
116+
fn above() {
117+
for a in [1, 2, 3] {
118+
let _: usize = a;
119+
}
120+
121+
for a in [String::new(), String::new()] {
122+
let _: String = a;
123+
}
124+
}
125+
126+
#[clippy::msrv = "1.52"]
127+
fn below() {
128+
for a in vec![1, 2, 3] {
129+
let _: usize = a;
130+
}
131+
132+
for a in vec![String::new(), String::new()] {
133+
let _: String = a;
134+
}
135+
}

tests/ui/vec.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
//@run-rustfix
22
#![warn(clippy::useless_vec)]
3-
#![allow(clippy::nonstandard_macro_braces, clippy::uninlined_format_args)]
3+
#![allow(clippy::nonstandard_macro_braces, clippy::uninlined_format_args, unused)]
44

55
use std::rc::Rc;
66

77
struct StructWithVec {
88
_x: Vec<i32>,
99
}
1010

11-
#[derive(Debug)]
12-
struct NonCopy;
13-
1411
fn on_slice(_: &[u8]) {}
1512

1613
fn on_mut_slice(_: &mut [u8]) {}
@@ -66,14 +63,6 @@ fn main() {
6663
on_mut_slice(&mut vec![2; line.length]);
6764
on_mut_slice(&mut vec![2; line.length()]);
6865

69-
for a in vec![1, 2, 3] {
70-
println!("{:?}", a);
71-
}
72-
73-
for a in vec![NonCopy, NonCopy] {
74-
println!("{:?}", a);
75-
}
76-
7766
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7867
on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7968

@@ -122,3 +111,25 @@ fn main() {
122111
// Too large
123112
let _x = vec![1; 201];
124113
}
114+
115+
#[clippy::msrv = "1.53"]
116+
fn above() {
117+
for a in vec![1, 2, 3] {
118+
let _: usize = a;
119+
}
120+
121+
for a in vec![String::new(), String::new()] {
122+
let _: String = a;
123+
}
124+
}
125+
126+
#[clippy::msrv = "1.52"]
127+
fn below() {
128+
for a in vec![1, 2, 3] {
129+
let _: usize = a;
130+
}
131+
132+
for a in vec![String::new(), String::new()] {
133+
let _: String = a;
134+
}
135+
}

tests/ui/vec.stderr

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,94 @@
11
error: useless use of `vec!`
2-
--> $DIR/vec.rs:34:14
2+
--> $DIR/vec.rs:31:14
33
|
44
LL | on_slice(&vec![]);
55
| ^^^^^^^ help: you can use a slice directly: `&[]`
66
|
77
= note: `-D clippy::useless-vec` implied by `-D warnings`
88

99
error: useless use of `vec!`
10-
--> $DIR/vec.rs:36:18
10+
--> $DIR/vec.rs:33:18
1111
|
1212
LL | on_mut_slice(&mut vec![]);
1313
| ^^^^^^^^^^^ help: you can use a slice directly: `&mut []`
1414

1515
error: useless use of `vec!`
16-
--> $DIR/vec.rs:38:14
16+
--> $DIR/vec.rs:35:14
1717
|
1818
LL | on_slice(&vec![1, 2]);
1919
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
2020

2121
error: useless use of `vec!`
22-
--> $DIR/vec.rs:40:18
22+
--> $DIR/vec.rs:37:18
2323
|
2424
LL | on_mut_slice(&mut vec![1, 2]);
2525
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
2626

2727
error: useless use of `vec!`
28-
--> $DIR/vec.rs:42:14
28+
--> $DIR/vec.rs:39:14
2929
|
3030
LL | on_slice(&vec![1, 2]);
3131
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
3232

3333
error: useless use of `vec!`
34-
--> $DIR/vec.rs:44:18
34+
--> $DIR/vec.rs:41:18
3535
|
3636
LL | on_mut_slice(&mut vec![1, 2]);
3737
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
3838

3939
error: useless use of `vec!`
40-
--> $DIR/vec.rs:46:14
40+
--> $DIR/vec.rs:43:14
4141
|
4242
LL | on_slice(&vec!(1, 2));
4343
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
4444

4545
error: useless use of `vec!`
46-
--> $DIR/vec.rs:48:18
46+
--> $DIR/vec.rs:45:18
4747
|
4848
LL | on_mut_slice(&mut vec![1, 2]);
4949
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
5050

5151
error: useless use of `vec!`
52-
--> $DIR/vec.rs:50:14
52+
--> $DIR/vec.rs:47:14
5353
|
5454
LL | on_slice(&vec![1; 2]);
5555
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1; 2]`
5656

5757
error: useless use of `vec!`
58-
--> $DIR/vec.rs:52:18
58+
--> $DIR/vec.rs:49:18
5959
|
6060
LL | on_mut_slice(&mut vec![1; 2]);
6161
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1; 2]`
6262

6363
error: useless use of `vec!`
64-
--> $DIR/vec.rs:69:14
65-
|
66-
LL | for a in vec![1, 2, 3] {
67-
| ^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2, 3]`
68-
69-
error: useless use of `vec!`
70-
--> $DIR/vec.rs:86:17
64+
--> $DIR/vec.rs:75:17
7165
|
7266
LL | let mut x = vec![1, 2, 3];
7367
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
7468

7569
error: useless use of `vec!`
76-
--> $DIR/vec.rs:92:22
70+
--> $DIR/vec.rs:81:22
7771
|
7872
LL | let _x: &[i32] = &vec![1, 2, 3];
7973
| ^^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2, 3]`
8074

8175
error: useless use of `vec!`
82-
--> $DIR/vec.rs:94:14
76+
--> $DIR/vec.rs:83:14
8377
|
8478
LL | for _ in vec![1, 2, 3] {}
85-
| ^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2, 3]`
79+
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
80+
81+
error: useless use of `vec!`
82+
--> $DIR/vec.rs:117:14
83+
|
84+
LL | for a in vec![1, 2, 3] {
85+
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
86+
87+
error: useless use of `vec!`
88+
--> $DIR/vec.rs:121:14
89+
|
90+
LL | for a in vec![String::new(), String::new()] {
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[String::new(), String::new()]`
8692

87-
error: aborting due to 14 previous errors
93+
error: aborting due to 15 previous errors
8894

0 commit comments

Comments
 (0)