mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 17:33:07 +00:00
tests for needless_range_loop
This commit is contained in:
parent
52bd7bb662
commit
1dc0b5c9ec
27
tests/ui/needless_range_loop.rs
Normal file
27
tests/ui/needless_range_loop.rs
Normal 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]);
|
||||
}
|
||||
}
|
14
tests/ui/needless_range_loop.stderr
Normal file
14
tests/ui/needless_range_loop.stderr
Normal 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) {
|
||||
| ^^^^^^
|
||||
|
Loading…
Reference in New Issue
Block a user