Auto merge of #11862 - christophbeberweil:7125-single-element-loop-over-range, r=llogiq
suggest alternatives to iterate an array of ranges works towards #7125 changelog: [`single_element_loop`]: suggest better syntax when iterating over an array of a single range `@thinkerdreamer` and myself worked on this issue during a workshop by `@llogiq` at the RustLab 2023 conference. It is our first contribution to clippy. When iterating over an array of only one element, _which is a range_, our change suggests to replace the array with the contained range itself. Additionally, a hint is printed stating that the user probably intended to iterate over the range and not the array. If the single element in the array is not a range, the previous suggestion in the form of `let {pat_snip} = {prefix}{arg_snip};{block_str}`is used. This change lints the array with the single range directly, so any prefixes or suffixes are covered as well.
This commit is contained in:
commit
6cfbe57075
3 changed files with 45 additions and 74 deletions
|
|
@ -15,23 +15,19 @@ fn main() {
|
|||
dbg!(item);
|
||||
}
|
||||
|
||||
{
|
||||
let item = &(0..5);
|
||||
for item in 0..5 {
|
||||
dbg!(item);
|
||||
}
|
||||
|
||||
{
|
||||
let item = &mut (0..5);
|
||||
for item in 0..5 {
|
||||
dbg!(item);
|
||||
}
|
||||
|
||||
{
|
||||
let item = 0..5;
|
||||
for item in 0..5 {
|
||||
dbg!(item);
|
||||
}
|
||||
|
||||
{
|
||||
let item = 0..5;
|
||||
for item in 0..5 {
|
||||
dbg!(item);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,69 +32,29 @@ LL + dbg!(item);
|
|||
LL + }
|
||||
|
|
||||
|
||||
error: for loop over a single element
|
||||
--> $DIR/single_element_loop.rs:16:5
|
||||
|
|
||||
LL | / for item in &[0..5] {
|
||||
LL | | dbg!(item);
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
help: try
|
||||
|
|
||||
LL ~ {
|
||||
LL + let item = &(0..5);
|
||||
LL + dbg!(item);
|
||||
LL + }
|
||||
error: this loops only once with `item` being `0..5`
|
||||
--> $DIR/single_element_loop.rs:16:17
|
||||
|
|
||||
LL | for item in &[0..5] {
|
||||
| ^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
|
||||
|
||||
error: for loop over a single element
|
||||
--> $DIR/single_element_loop.rs:20:5
|
||||
|
|
||||
LL | / for item in [0..5].iter_mut() {
|
||||
LL | | dbg!(item);
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
help: try
|
||||
|
|
||||
LL ~ {
|
||||
LL + let item = &mut (0..5);
|
||||
LL + dbg!(item);
|
||||
LL + }
|
||||
error: this loops only once with `item` being `0..5`
|
||||
--> $DIR/single_element_loop.rs:20:17
|
||||
|
|
||||
LL | for item in [0..5].iter_mut() {
|
||||
| ^^^^^^^^^^^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
|
||||
|
||||
error: for loop over a single element
|
||||
--> $DIR/single_element_loop.rs:24:5
|
||||
|
|
||||
LL | / for item in [0..5] {
|
||||
LL | | dbg!(item);
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
help: try
|
||||
|
|
||||
LL ~ {
|
||||
LL + let item = 0..5;
|
||||
LL + dbg!(item);
|
||||
LL + }
|
||||
error: this loops only once with `item` being `0..5`
|
||||
--> $DIR/single_element_loop.rs:24:17
|
||||
|
|
||||
LL | for item in [0..5] {
|
||||
| ^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
|
||||
|
||||
error: for loop over a single element
|
||||
--> $DIR/single_element_loop.rs:28:5
|
||||
|
|
||||
LL | / for item in [0..5].into_iter() {
|
||||
LL | | dbg!(item);
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
help: try
|
||||
|
|
||||
LL ~ {
|
||||
LL + let item = 0..5;
|
||||
LL + dbg!(item);
|
||||
LL + }
|
||||
error: this loops only once with `item` being `0..5`
|
||||
--> $DIR/single_element_loop.rs:28:17
|
||||
|
|
||||
LL | for item in [0..5].into_iter() {
|
||||
| ^^^^^^^^^^^^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
|
||||
|
||||
error: for loop over a single element
|
||||
--> $DIR/single_element_loop.rs:47:5
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue