Skip to content

Commit 1dc0b5c

Browse files
committed
tests for needless_range_loop
1 parent 52bd7bb commit 1dc0b5c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

tests/ui/needless_range_loop.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
fn calc_idx(i: usize) -> usize {
2+
(i + i + 20) % 4
3+
}
4+
5+
fn main() {
6+
let ns = [2, 3, 5, 7];
7+
8+
for i in 3..10 {
9+
println!("{}", ns[i]);
10+
}
11+
12+
for i in 3..10 {
13+
println!("{}", ns[i % 4]);
14+
}
15+
16+
for i in 3..10 {
17+
println!("{}", ns[i % ns.len()]);
18+
}
19+
20+
for i in 3..10 {
21+
println!("{}", ns[calc_idx(i)]);
22+
}
23+
24+
for i in 3..10 {
25+
println!("{}", ns[calc_idx(i) % 4]);
26+
}
27+
}

tests/ui/needless_range_loop.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the loop variable `i` is only used to index `ns`.
2+
--> $DIR/needless_range_loop.rs:8:5
3+
|
4+
8 | / for i in 3..10 {
5+
9 | | println!("{}", ns[i]);
6+
10 | | }
7+
| |_____^
8+
|
9+
= note: `-D needless-range-loop` implied by `-D warnings`
10+
help: consider using an iterator
11+
|
12+
8 | for <item> in ns.iter().take(10).skip(3) {
13+
| ^^^^^^
14+

0 commit comments

Comments
 (0)