Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 447edf9

Browse files
suggest alternatives to iterate an array of ranges
Co-authored-by: ThinkerDreamer <74881094+ThinkerDreamer@users.noreply.github.com>
1 parent 840e227 commit 447edf9

File tree

3 files changed

+43
-74
lines changed

3 files changed

+43
-74
lines changed

clippy_lints/src/loops/single_element_loop.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::SINGLE_ELEMENT_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::source::{indent_of, snippet_with_applicability};
3+
use clippy_utils::source::{indent_of, snippet, snippet_with_applicability};
44
use clippy_utils::visitors::contains_break_or_continue;
55
use rustc_ast::util::parser::PREC_PREFIX;
66
use rustc_ast::Mutability;
@@ -87,14 +87,27 @@ pub(super) fn check<'tcx>(
8787
arg_snip = format!("({arg_snip})").into();
8888
}
8989

90-
span_lint_and_sugg(
91-
cx,
92-
SINGLE_ELEMENT_LOOP,
93-
expr.span,
94-
"for loop over a single element",
95-
"try",
96-
format!("{{\n{indent}let {pat_snip} = {prefix}{arg_snip};{block_str}}}"),
97-
applicability,
98-
);
90+
if clippy_utils::higher::Range::hir(arg_expression).is_some() {
91+
let sugg = snippet(cx, arg_expression.span, "..");
92+
span_lint_and_sugg(
93+
cx,
94+
SINGLE_ELEMENT_LOOP,
95+
arg.span,
96+
"for loop over a single range inside an array, rather than iterating over the elements in the range directly",
97+
"did you mean to iterate over the range instead?",
98+
sugg.to_string(),
99+
applicability,
100+
);
101+
} else {
102+
span_lint_and_sugg(
103+
cx,
104+
SINGLE_ELEMENT_LOOP,
105+
expr.span,
106+
"for loop over a single element",
107+
"try",
108+
format!("{{\n{indent}let {pat_snip} = {prefix}{arg_snip};{block_str}}}"),
109+
applicability,
110+
);
111+
}
99112
}
100113
}

tests/ui/single_element_loop.fixed

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,19 @@ fn main() {
1515
dbg!(item);
1616
}
1717

18-
{
19-
let item = &(0..5);
18+
for item in 0..5 {
2019
dbg!(item);
2120
}
2221

23-
{
24-
let item = &mut (0..5);
22+
for item in 0..5 {
2523
dbg!(item);
2624
}
2725

28-
{
29-
let item = 0..5;
26+
for item in 0..5 {
3027
dbg!(item);
3128
}
3229

33-
{
34-
let item = 0..5;
30+
for item in 0..5 {
3531
dbg!(item);
3632
}
3733

tests/ui/single_element_loop.stderr

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,69 +32,29 @@ LL + dbg!(item);
3232
LL + }
3333
|
3434

35-
error: for loop over a single element
36-
--> $DIR/single_element_loop.rs:16:5
37-
|
38-
LL | / for item in &[0..5] {
39-
LL | | dbg!(item);
40-
LL | | }
41-
| |_____^
42-
|
43-
help: try
44-
|
45-
LL ~ {
46-
LL + let item = &(0..5);
47-
LL + dbg!(item);
48-
LL + }
35+
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
36+
--> $DIR/single_element_loop.rs:16:17
4937
|
38+
LL | for item in &[0..5] {
39+
| ^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
5040

51-
error: for loop over a single element
52-
--> $DIR/single_element_loop.rs:20:5
53-
|
54-
LL | / for item in [0..5].iter_mut() {
55-
LL | | dbg!(item);
56-
LL | | }
57-
| |_____^
58-
|
59-
help: try
60-
|
61-
LL ~ {
62-
LL + let item = &mut (0..5);
63-
LL + dbg!(item);
64-
LL + }
41+
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
42+
--> $DIR/single_element_loop.rs:20:17
6543
|
44+
LL | for item in [0..5].iter_mut() {
45+
| ^^^^^^^^^^^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
6646

67-
error: for loop over a single element
68-
--> $DIR/single_element_loop.rs:24:5
69-
|
70-
LL | / for item in [0..5] {
71-
LL | | dbg!(item);
72-
LL | | }
73-
| |_____^
74-
|
75-
help: try
76-
|
77-
LL ~ {
78-
LL + let item = 0..5;
79-
LL + dbg!(item);
80-
LL + }
47+
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
48+
--> $DIR/single_element_loop.rs:24:17
8149
|
50+
LL | for item in [0..5] {
51+
| ^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
8252

83-
error: for loop over a single element
84-
--> $DIR/single_element_loop.rs:28:5
85-
|
86-
LL | / for item in [0..5].into_iter() {
87-
LL | | dbg!(item);
88-
LL | | }
89-
| |_____^
90-
|
91-
help: try
92-
|
93-
LL ~ {
94-
LL + let item = 0..5;
95-
LL + dbg!(item);
96-
LL + }
53+
error: for loop over a single range inside an array, rather than iterating over the elements in the range directly
54+
--> $DIR/single_element_loop.rs:28:17
9755
|
56+
LL | for item in [0..5].into_iter() {
57+
| ^^^^^^^^^^^^^^^^^^ help: did you mean to iterate over the range instead?: `0..5`
9858

9959
error: for loop over a single element
10060
--> $DIR/single_element_loop.rs:47:5

0 commit comments

Comments
 (0)