Auto merge of #10934 - Centri3:single_range_in_vec_init, r=giraffate

new lint [`single_range_in_vec_init`]

Lints on `vec![0..200]` (or `[0..200]`), suggesting either `(0..200).collect::<Vec<i32>>()` or `[0; 200]`.

Haven't tested it with anything that isn't primitive. Probably should!

Closes #10932

changelog: new lint [`single_range_in_vec_init`]
This commit is contained in:
bors 2023-06-14 23:57:03 +00:00
commit 823d9dd503
12 changed files with 393 additions and 33 deletions

View file

@ -1,6 +1,8 @@
//@run-rustfix
// Tests from for_loop.rs that don't have suggestions
#![allow(clippy::single_range_in_vec_init)]
#[warn(clippy::single_element_loop)]
fn main() {
let item1 = 2;

View file

@ -1,6 +1,8 @@
//@run-rustfix
// Tests from for_loop.rs that don't have suggestions
#![allow(clippy::single_range_in_vec_init)]
#[warn(clippy::single_element_loop)]
fn main() {
let item1 = 2;

View file

@ -1,5 +1,5 @@
error: for loop over a single element
--> $DIR/single_element_loop.rs:7:5
--> $DIR/single_element_loop.rs:9:5
|
LL | / for item in &[item1] {
LL | | dbg!(item);
@ -16,7 +16,7 @@ LL + }
|
error: for loop over a single element
--> $DIR/single_element_loop.rs:11:5
--> $DIR/single_element_loop.rs:13:5
|
LL | / for item in [item1].iter() {
LL | | dbg!(item);
@ -32,7 +32,7 @@ LL + }
|
error: for loop over a single element
--> $DIR/single_element_loop.rs:15:5
--> $DIR/single_element_loop.rs:17:5
|
LL | / for item in &[0..5] {
LL | | dbg!(item);
@ -48,7 +48,7 @@ LL + }
|
error: for loop over a single element
--> $DIR/single_element_loop.rs:19:5
--> $DIR/single_element_loop.rs:21:5
|
LL | / for item in [0..5].iter_mut() {
LL | | dbg!(item);
@ -64,7 +64,7 @@ LL + }
|
error: for loop over a single element
--> $DIR/single_element_loop.rs:23:5
--> $DIR/single_element_loop.rs:25:5
|
LL | / for item in [0..5] {
LL | | dbg!(item);
@ -80,7 +80,7 @@ LL + }
|
error: for loop over a single element
--> $DIR/single_element_loop.rs:27:5
--> $DIR/single_element_loop.rs:29:5
|
LL | / for item in [0..5].into_iter() {
LL | | dbg!(item);
@ -96,7 +96,7 @@ LL + }
|
error: for loop over a single element
--> $DIR/single_element_loop.rs:46:5
--> $DIR/single_element_loop.rs:48:5
|
LL | / for _ in [42] {
LL | | let _f = |n: u32| {

View file

@ -0,0 +1,58 @@
//@aux-build:proc_macros.rs
#![allow(clippy::no_effect, clippy::useless_vec, unused)]
#![warn(clippy::single_range_in_vec_init)]
#![feature(generic_arg_infer)]
#[macro_use]
extern crate proc_macros;
macro_rules! a {
() => {
vec![0..200];
};
}
fn awa<T: PartialOrd>(start: T, end: T) {
[start..end];
}
fn awa_vec<T: PartialOrd>(start: T, end: T) {
vec![start..end];
}
fn main() {
// Lint
[0..200];
vec![0..200];
[0u8..200];
[0usize..200];
[0..200usize];
vec![0u8..200];
vec![0usize..200];
vec![0..200usize];
// Only suggest collect
[0..200isize];
vec![0..200isize];
// Do not lint
[0..200, 0..100];
vec![0..200, 0..100];
[0.0..200.0];
vec![0.0..200.0];
// `Copy` is not implemented for `Range`, so this doesn't matter
// [0..200; 2];
// [vec!0..200; 2];
// Unfortunately skips any macros
a!();
// Skip external macros and procedural macros
external! {
[0..200];
vec![0..200];
}
with_span! {
span
[0..200];
vec![0..200];
}
}

View file

@ -0,0 +1,145 @@
error: an array of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:25:5
|
LL | [0..200];
| ^^^^^^^^
|
= note: `-D clippy::single-range-in-vec-init` implied by `-D warnings`
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0..200).collect::<std::vec::Vec<i32>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted an array of len 200, try
|
LL | [0; 200];
| ~~~~~~
error: a `Vec` of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:26:5
|
LL | vec![0..200];
| ^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0..200).collect::<std::vec::Vec<i32>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted a `Vec` of len 200, try
|
LL | vec![0; 200];
| ~~~~~~
error: an array of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:27:5
|
LL | [0u8..200];
| ^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0u8..200).collect::<std::vec::Vec<u8>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted an array of len 200, try
|
LL | [0u8; 200];
| ~~~~~~~~
error: an array of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:28:5
|
LL | [0usize..200];
| ^^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0usize..200).collect::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted an array of len 200, try
|
LL | [0usize; 200];
| ~~~~~~~~~~~
error: an array of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:29:5
|
LL | [0..200usize];
| ^^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0..200usize).collect::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted an array of len 200usize, try
|
LL | [0; 200usize];
| ~~~~~~~~~~~
error: a `Vec` of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:30:5
|
LL | vec![0u8..200];
| ^^^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0u8..200).collect::<std::vec::Vec<u8>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted a `Vec` of len 200, try
|
LL | vec![0u8; 200];
| ~~~~~~~~
error: a `Vec` of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:31:5
|
LL | vec![0usize..200];
| ^^^^^^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0usize..200).collect::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted a `Vec` of len 200, try
|
LL | vec![0usize; 200];
| ~~~~~~~~~~~
error: a `Vec` of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:32:5
|
LL | vec![0..200usize];
| ^^^^^^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0..200usize).collect::<std::vec::Vec<usize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: if you wanted a `Vec` of len 200usize, try
|
LL | vec![0; 200usize];
| ~~~~~~~~~~~
error: an array of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:34:5
|
LL | [0..200isize];
| ^^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0..200isize).collect::<std::vec::Vec<isize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: a `Vec` of `Range` that is only one element
--> $DIR/single_range_in_vec_init.rs:35:5
|
LL | vec![0..200isize];
| ^^^^^^^^^^^^^^^^^
|
help: if you wanted a `Vec` that contains the entire range, try
|
LL | (0..200isize).collect::<std::vec::Vec<isize>>();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 10 previous errors

View file

@ -6,6 +6,7 @@
clippy::manual_find,
clippy::never_loop,
clippy::redundant_closure_call,
clippy::single_range_in_vec_init,
clippy::uninlined_format_args,
clippy::useless_vec
)]

View file

@ -6,6 +6,7 @@
clippy::manual_find,
clippy::never_loop,
clippy::redundant_closure_call,
clippy::single_range_in_vec_init,
clippy::uninlined_format_args,
clippy::useless_vec
)]

View file

@ -1,5 +1,5 @@
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:15:5
--> $DIR/while_let_on_iterator.rs:16:5
|
LL | while let Option::Some(x) = iter.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in iter`
@ -7,151 +7,151 @@ LL | while let Option::Some(x) = iter.next() {
= note: `-D clippy::while-let-on-iterator` implied by `-D warnings`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:20:5
--> $DIR/while_let_on_iterator.rs:21:5
|
LL | while let Some(x) = iter.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in iter`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:25:5
--> $DIR/while_let_on_iterator.rs:26:5
|
LL | while let Some(_) = iter.next() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in iter`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:101:9
--> $DIR/while_let_on_iterator.rs:102:9
|
LL | while let Some([..]) = it.next() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for [..] in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:108:9
--> $DIR/while_let_on_iterator.rs:109:9
|
LL | while let Some([_x]) = it.next() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for [_x] in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:121:9
--> $DIR/while_let_on_iterator.rs:122:9
|
LL | while let Some(x @ [_]) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x @ [_] in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:141:9
--> $DIR/while_let_on_iterator.rs:142:9
|
LL | while let Some(_) = y.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in y`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:198:9
--> $DIR/while_let_on_iterator.rs:199:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:209:5
--> $DIR/while_let_on_iterator.rs:210:5
|
LL | while let Some(n) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for n in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:211:9
--> $DIR/while_let_on_iterator.rs:212:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:220:9
--> $DIR/while_let_on_iterator.rs:221:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:229:9
--> $DIR/while_let_on_iterator.rs:230:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:246:9
--> $DIR/while_let_on_iterator.rs:247:9
|
LL | while let Some(m) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for m in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:261:13
--> $DIR/while_let_on_iterator.rs:262:13
|
LL | while let Some(i) = self.0.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for i in self.0.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:293:13
--> $DIR/while_let_on_iterator.rs:294:13
|
LL | while let Some(i) = self.0.0.0.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for i in self.0.0.0.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:322:5
--> $DIR/while_let_on_iterator.rs:323:5
|
LL | while let Some(n) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for n in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:334:9
--> $DIR/while_let_on_iterator.rs:335:9
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:348:5
--> $DIR/while_let_on_iterator.rs:349:5
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:359:5
--> $DIR/while_let_on_iterator.rs:360:5
|
LL | while let Some(x) = it.0.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it.0.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:394:5
--> $DIR/while_let_on_iterator.rs:395:5
|
LL | while let Some(x) = s.x.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in s.x.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:401:5
--> $DIR/while_let_on_iterator.rs:402:5
|
LL | while let Some(x) = x[0].next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in x[0].by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:409:9
--> $DIR/while_let_on_iterator.rs:410:9
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:419:9
--> $DIR/while_let_on_iterator.rs:420:9
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:429:9
--> $DIR/while_let_on_iterator.rs:430:9
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it.by_ref()`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:439:9
--> $DIR/while_let_on_iterator.rs:440:9
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:449:5
--> $DIR/while_let_on_iterator.rs:450:5
|
LL | while let Some(..) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in it`