tests for `needless_range_loop`

This commit is contained in:
Andriy S. from cobalt 2017-10-08 22:37:04 +03:00
parent 52bd7bb662
commit 1dc0b5c9ec
2 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,27 @@
fn calc_idx(i: usize) -> usize {
(i + i + 20) % 4
}
fn main() {
let ns = [2, 3, 5, 7];
for i in 3..10 {
println!("{}", ns[i]);
}
for i in 3..10 {
println!("{}", ns[i % 4]);
}
for i in 3..10 {
println!("{}", ns[i % ns.len()]);
}
for i in 3..10 {
println!("{}", ns[calc_idx(i)]);
}
for i in 3..10 {
println!("{}", ns[calc_idx(i) % 4]);
}
}

View File

@ -0,0 +1,14 @@
error: the loop variable `i` is only used to index `ns`.
--> $DIR/needless_range_loop.rs:8:5
|
8 | / for i in 3..10 {
9 | | println!("{}", ns[i]);
10 | | }
| |_____^
|
= note: `-D needless-range-loop` implied by `-D warnings`
help: consider using an iterator
|
8 | for <item> in ns.iter().take(10).skip(3) {
| ^^^^^^