mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 05:03:21 +00:00
Split up needless_range_loop
ui test
This commit is contained in:
parent
2c7cfa8321
commit
46b787d1b8
4 changed files with 194 additions and 189 deletions
|
@ -1,93 +1,10 @@
|
|||
#![allow(clippy::cognitive_complexity)]
|
||||
#![warn(clippy::needless_range_loop)]
|
||||
|
||||
static STATIC: [usize; 4] = [0, 1, 8, 16];
|
||||
const CONST: [usize; 4] = [0, 1, 8, 16];
|
||||
|
||||
fn calc_idx(i: usize) -> usize {
|
||||
(i + i + 20) % 4
|
||||
}
|
||||
const MAX_LEN: usize = 42;
|
||||
|
||||
fn main() {
|
||||
const MAX_LEN: usize = 42;
|
||||
|
||||
let ns = vec![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]);
|
||||
}
|
||||
|
||||
let mut ms = vec![1, 2, 3, 4, 5, 6];
|
||||
for i in 0..ms.len() {
|
||||
ms[i] *= 2;
|
||||
}
|
||||
assert_eq!(ms, vec![2, 4, 6, 8, 10, 12]);
|
||||
|
||||
let mut ms = vec![1, 2, 3, 4, 5, 6];
|
||||
for i in 0..ms.len() {
|
||||
let x = &mut ms[i];
|
||||
*x *= 2;
|
||||
}
|
||||
assert_eq!(ms, vec![2, 4, 6, 8, 10, 12]);
|
||||
|
||||
let g = vec![1, 2, 3, 4, 5, 6];
|
||||
let glen = g.len();
|
||||
for i in 0..glen {
|
||||
let x: u32 = g[i + 1..].iter().sum();
|
||||
println!("{}", g[i] + x);
|
||||
}
|
||||
assert_eq!(g, vec![20, 18, 15, 11, 6, 0]);
|
||||
|
||||
let mut g = vec![1, 2, 3, 4, 5, 6];
|
||||
let glen = g.len();
|
||||
for i in 0..glen {
|
||||
g[i] = g[i + 1..].iter().sum();
|
||||
}
|
||||
assert_eq!(g, vec![20, 18, 15, 11, 6, 0]);
|
||||
|
||||
let x = 5;
|
||||
let mut vec = vec![0; 9];
|
||||
|
||||
for i in x..x + 4 {
|
||||
vec[i] += 1;
|
||||
}
|
||||
|
||||
let x = 5;
|
||||
let mut vec = vec![0; 10];
|
||||
|
||||
for i in x..=x + 4 {
|
||||
vec[i] += 1;
|
||||
}
|
||||
|
||||
let arr = [1, 2, 3];
|
||||
|
||||
for i in 0..3 {
|
||||
println!("{}", arr[i]);
|
||||
}
|
||||
|
||||
for i in 0..2 {
|
||||
println!("{}", arr[i]);
|
||||
}
|
||||
|
||||
for i in 1..3 {
|
||||
println!("{}", arr[i]);
|
||||
}
|
||||
|
||||
let mut vec = vec![1, 2, 3, 4];
|
||||
let vec2 = vec![1, 2, 3, 4];
|
||||
for i in 0..vec.len() {
|
||||
|
|
|
@ -1,105 +1,17 @@
|
|||
error: the loop variable `i` is only used to index `ns`.
|
||||
--> $DIR/needless_range_loop.rs:15:14
|
||||
|
|
||||
LL | for i in 3..10 {
|
||||
| ^^^^^
|
||||
|
|
||||
= note: `-D clippy::needless-range-loop` implied by `-D warnings`
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in ns.iter().take(10).skip(3) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `ms`.
|
||||
--> $DIR/needless_range_loop.rs:36:14
|
||||
|
|
||||
LL | for i in 0..ms.len() {
|
||||
| ^^^^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in &mut ms {
|
||||
| ^^^^^^ ^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `ms`.
|
||||
--> $DIR/needless_range_loop.rs:42:14
|
||||
|
|
||||
LL | for i in 0..ms.len() {
|
||||
| ^^^^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in &mut ms {
|
||||
| ^^^^^^ ^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:66:14
|
||||
|
|
||||
LL | for i in x..x + 4 {
|
||||
| ^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in vec.iter_mut().skip(x).take(4) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:73:14
|
||||
|
|
||||
LL | for i in x..=x + 4 {
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in vec.iter_mut().skip(x).take(4 + 1) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `arr`.
|
||||
--> $DIR/needless_range_loop.rs:79:14
|
||||
|
|
||||
LL | for i in 0..3 {
|
||||
| ^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in &arr {
|
||||
| ^^^^^^ ^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `arr`.
|
||||
--> $DIR/needless_range_loop.rs:83:14
|
||||
|
|
||||
LL | for i in 0..2 {
|
||||
| ^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in arr.iter().take(2) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `arr`.
|
||||
--> $DIR/needless_range_loop.rs:87:14
|
||||
|
|
||||
LL | for i in 1..3 {
|
||||
| ^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in arr.iter().skip(1) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:93:14
|
||||
--> $DIR/needless_range_loop.rs:10:14
|
||||
|
|
||||
LL | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::needless-range-loop` implied by `-D warnings`
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in &vec {
|
||||
| ^^^^^^ ^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:102:14
|
||||
--> $DIR/needless_range_loop.rs:19:14
|
||||
|
|
||||
LL | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
@ -110,7 +22,7 @@ LL | for <item> in &vec {
|
|||
| ^^^^^^ ^^^^
|
||||
|
||||
error: the loop variable `j` is only used to index `STATIC`.
|
||||
--> $DIR/needless_range_loop.rs:107:14
|
||||
--> $DIR/needless_range_loop.rs:24:14
|
||||
|
|
||||
LL | for j in 0..4 {
|
||||
| ^^^^
|
||||
|
@ -121,7 +33,7 @@ LL | for <item> in &STATIC {
|
|||
| ^^^^^^ ^^^^^^^
|
||||
|
||||
error: the loop variable `j` is only used to index `CONST`.
|
||||
--> $DIR/needless_range_loop.rs:111:14
|
||||
--> $DIR/needless_range_loop.rs:28:14
|
||||
|
|
||||
LL | for j in 0..4 {
|
||||
| ^^^^
|
||||
|
@ -132,7 +44,7 @@ LL | for <item> in &CONST {
|
|||
| ^^^^^^ ^^^^^^
|
||||
|
||||
error: the loop variable `i` is used to index `vec`
|
||||
--> $DIR/needless_range_loop.rs:115:14
|
||||
--> $DIR/needless_range_loop.rs:32:14
|
||||
|
|
||||
LL | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
@ -143,7 +55,7 @@ LL | for (i, <item>) in vec.iter().enumerate() {
|
|||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec2`.
|
||||
--> $DIR/needless_range_loop.rs:123:14
|
||||
--> $DIR/needless_range_loop.rs:40:14
|
||||
|
|
||||
LL | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
@ -154,7 +66,7 @@ LL | for <item> in vec2.iter().take(vec.len()) {
|
|||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:127:14
|
||||
--> $DIR/needless_range_loop.rs:44:14
|
||||
|
|
||||
LL | for i in 5..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
@ -165,7 +77,7 @@ LL | for <item> in vec.iter().skip(5) {
|
|||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:131:14
|
||||
--> $DIR/needless_range_loop.rs:48:14
|
||||
|
|
||||
LL | for i in 0..MAX_LEN {
|
||||
| ^^^^^^^^^^
|
||||
|
@ -176,7 +88,7 @@ LL | for <item> in vec.iter().take(MAX_LEN) {
|
|||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:135:14
|
||||
--> $DIR/needless_range_loop.rs:52:14
|
||||
|
|
||||
LL | for i in 0..=MAX_LEN {
|
||||
| ^^^^^^^^^^^
|
||||
|
@ -187,7 +99,7 @@ LL | for <item> in vec.iter().take(MAX_LEN + 1) {
|
|||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:139:14
|
||||
--> $DIR/needless_range_loop.rs:56:14
|
||||
|
|
||||
LL | for i in 5..10 {
|
||||
| ^^^^^
|
||||
|
@ -198,7 +110,7 @@ LL | for <item> in vec.iter().take(10).skip(5) {
|
|||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop.rs:143:14
|
||||
--> $DIR/needless_range_loop.rs:60:14
|
||||
|
|
||||
LL | for i in 5..=10 {
|
||||
| ^^^^^^
|
||||
|
@ -209,7 +121,7 @@ LL | for <item> in vec.iter().take(10 + 1).skip(5) {
|
|||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is used to index `vec`
|
||||
--> $DIR/needless_range_loop.rs:147:14
|
||||
--> $DIR/needless_range_loop.rs:64:14
|
||||
|
|
||||
LL | for i in 5..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
@ -220,7 +132,7 @@ LL | for (i, <item>) in vec.iter().enumerate().skip(5) {
|
|||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is used to index `vec`
|
||||
--> $DIR/needless_range_loop.rs:151:14
|
||||
--> $DIR/needless_range_loop.rs:68:14
|
||||
|
|
||||
LL | for i in 5..10 {
|
||||
| ^^^^^
|
||||
|
@ -231,7 +143,7 @@ LL | for (i, <item>) in vec.iter().enumerate().take(10).skip(5) {
|
|||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is used to index `vec`
|
||||
--> $DIR/needless_range_loop.rs:156:14
|
||||
--> $DIR/needless_range_loop.rs:73:14
|
||||
|
|
||||
LL | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
@ -241,5 +153,5 @@ help: consider using an iterator
|
|||
LL | for (i, <item>) in vec.iter_mut().enumerate() {
|
||||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 22 previous errors
|
||||
error: aborting due to 14 previous errors
|
||||
|
||||
|
|
85
tests/ui/needless_range_loop2.rs
Normal file
85
tests/ui/needless_range_loop2.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
#![warn(clippy::needless_range_loop)]
|
||||
|
||||
fn calc_idx(i: usize) -> usize {
|
||||
(i + i + 20) % 4
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let ns = vec![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]);
|
||||
}
|
||||
|
||||
let mut ms = vec![1, 2, 3, 4, 5, 6];
|
||||
for i in 0..ms.len() {
|
||||
ms[i] *= 2;
|
||||
}
|
||||
assert_eq!(ms, vec![2, 4, 6, 8, 10, 12]);
|
||||
|
||||
let mut ms = vec![1, 2, 3, 4, 5, 6];
|
||||
for i in 0..ms.len() {
|
||||
let x = &mut ms[i];
|
||||
*x *= 2;
|
||||
}
|
||||
assert_eq!(ms, vec![2, 4, 6, 8, 10, 12]);
|
||||
|
||||
let g = vec![1, 2, 3, 4, 5, 6];
|
||||
let glen = g.len();
|
||||
for i in 0..glen {
|
||||
let x: u32 = g[i + 1..].iter().sum();
|
||||
println!("{}", g[i] + x);
|
||||
}
|
||||
assert_eq!(g, vec![20, 18, 15, 11, 6, 0]);
|
||||
|
||||
let mut g = vec![1, 2, 3, 4, 5, 6];
|
||||
let glen = g.len();
|
||||
for i in 0..glen {
|
||||
g[i] = g[i + 1..].iter().sum();
|
||||
}
|
||||
assert_eq!(g, vec![20, 18, 15, 11, 6, 0]);
|
||||
|
||||
let x = 5;
|
||||
let mut vec = vec![0; 9];
|
||||
|
||||
for i in x..x + 4 {
|
||||
vec[i] += 1;
|
||||
}
|
||||
|
||||
let x = 5;
|
||||
let mut vec = vec![0; 10];
|
||||
|
||||
for i in x..=x + 4 {
|
||||
vec[i] += 1;
|
||||
}
|
||||
|
||||
let arr = [1, 2, 3];
|
||||
|
||||
for i in 0..3 {
|
||||
println!("{}", arr[i]);
|
||||
}
|
||||
|
||||
for i in 0..2 {
|
||||
println!("{}", arr[i]);
|
||||
}
|
||||
|
||||
for i in 1..3 {
|
||||
println!("{}", arr[i]);
|
||||
}
|
||||
}
|
91
tests/ui/needless_range_loop2.stderr
Normal file
91
tests/ui/needless_range_loop2.stderr
Normal file
|
@ -0,0 +1,91 @@
|
|||
error: the loop variable `i` is only used to index `ns`.
|
||||
--> $DIR/needless_range_loop2.rs:10:14
|
||||
|
|
||||
LL | for i in 3..10 {
|
||||
| ^^^^^
|
||||
|
|
||||
= note: `-D clippy::needless-range-loop` implied by `-D warnings`
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in ns.iter().take(10).skip(3) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `ms`.
|
||||
--> $DIR/needless_range_loop2.rs:31:14
|
||||
|
|
||||
LL | for i in 0..ms.len() {
|
||||
| ^^^^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in &mut ms {
|
||||
| ^^^^^^ ^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `ms`.
|
||||
--> $DIR/needless_range_loop2.rs:37:14
|
||||
|
|
||||
LL | for i in 0..ms.len() {
|
||||
| ^^^^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in &mut ms {
|
||||
| ^^^^^^ ^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop2.rs:61:14
|
||||
|
|
||||
LL | for i in x..x + 4 {
|
||||
| ^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in vec.iter_mut().skip(x).take(4) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/needless_range_loop2.rs:68:14
|
||||
|
|
||||
LL | for i in x..=x + 4 {
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in vec.iter_mut().skip(x).take(4 + 1) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `arr`.
|
||||
--> $DIR/needless_range_loop2.rs:74:14
|
||||
|
|
||||
LL | for i in 0..3 {
|
||||
| ^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in &arr {
|
||||
| ^^^^^^ ^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `arr`.
|
||||
--> $DIR/needless_range_loop2.rs:78:14
|
||||
|
|
||||
LL | for i in 0..2 {
|
||||
| ^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in arr.iter().take(2) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `arr`.
|
||||
--> $DIR/needless_range_loop2.rs:82:14
|
||||
|
|
||||
LL | for i in 1..3 {
|
||||
| ^^^^
|
||||
|
|
||||
help: consider using an iterator
|
||||
|
|
||||
LL | for <item> in arr.iter().skip(1) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
Loading…
Reference in a new issue