From 0fbe49d8a6ce6ca54780c59f1d547311a26fbedc Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Wed, 24 Apr 2019 08:01:16 +0200 Subject: [PATCH] UI test cleanup: Extract needless_range_loop tests --- tests/ui/for_loop.rs | 63 ---------- tests/ui/for_loop.stderr | 178 ++++------------------------ tests/ui/needless_range_loop.rs | 71 +++++++++++ tests/ui/needless_range_loop.stderr | 150 +++++++++++++++++++++-- 4 files changed, 234 insertions(+), 228 deletions(-) diff --git a/tests/ui/for_loop.rs b/tests/ui/for_loop.rs index 41ac824e869..f13f826b1cc 100644 --- a/tests/ui/for_loop.rs +++ b/tests/ui/for_loop.rs @@ -35,70 +35,7 @@ impl Unrelated { #[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)] fn main() { const MAX_LEN: usize = 42; - let mut vec = vec![1, 2, 3, 4]; - let vec2 = vec![1, 2, 3, 4]; - for i in 0..vec.len() { - println!("{}", vec[i]); - } - - for i in 0..vec.len() { - let i = 42; // make a different `i` - println!("{}", vec[i]); // ok, not the `i` of the for-loop - } - - for i in 0..vec.len() { - let _ = vec[i]; - } - - // ICE #746 - for j in 0..4 { - println!("{:?}", STATIC[j]); - } - - for j in 0..4 { - println!("{:?}", CONST[j]); - } - - for i in 0..vec.len() { - println!("{} {}", vec[i], i); - } - for i in 0..vec.len() { - // not an error, indexing more than one variable - println!("{} {}", vec[i], vec2[i]); - } - - for i in 0..vec.len() { - println!("{}", vec2[i]); - } - - for i in 5..vec.len() { - println!("{}", vec[i]); - } - - for i in 0..MAX_LEN { - println!("{}", vec[i]); - } - - for i in 0..=MAX_LEN { - println!("{}", vec[i]); - } - - for i in 5..10 { - println!("{}", vec[i]); - } - - for i in 5..=10 { - println!("{}", vec[i]); - } - - for i in 5..vec.len() { - println!("{} {}", vec[i], i); - } - - for i in 5..10 { - println!("{} {}", vec[i], i); - } for i in 10..0 { println!("{}", i); diff --git a/tests/ui/for_loop.stderr b/tests/ui/for_loop.stderr index c1ad2361484..a18eb20774d 100644 --- a/tests/ui/for_loop.stderr +++ b/tests/ui/for_loop.stderr @@ -1,137 +1,5 @@ -error: the loop variable `i` is only used to index `vec`. - --> $DIR/for_loop.rs:41: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 in &vec { - | ^^^^^^ ^^^^ - -error: the loop variable `i` is only used to index `vec`. - --> $DIR/for_loop.rs:50:14 - | -LL | for i in 0..vec.len() { - | ^^^^^^^^^^^^ -help: consider using an iterator - | -LL | for in &vec { - | ^^^^^^ ^^^^ - -error: the loop variable `j` is only used to index `STATIC`. - --> $DIR/for_loop.rs:55:14 - | -LL | for j in 0..4 { - | ^^^^ -help: consider using an iterator - | -LL | for in &STATIC { - | ^^^^^^ ^^^^^^^ - -error: the loop variable `j` is only used to index `CONST`. - --> $DIR/for_loop.rs:59:14 - | -LL | for j in 0..4 { - | ^^^^ -help: consider using an iterator - | -LL | for in &CONST { - | ^^^^^^ ^^^^^^ - -error: the loop variable `i` is used to index `vec` - --> $DIR/for_loop.rs:63:14 - | -LL | for i in 0..vec.len() { - | ^^^^^^^^^^^^ -help: consider using an iterator - | -LL | for (i, ) in vec.iter().enumerate() { - | ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is only used to index `vec2`. - --> $DIR/for_loop.rs:71:14 - | -LL | for i in 0..vec.len() { - | ^^^^^^^^^^^^ -help: consider using an iterator - | -LL | for in vec2.iter().take(vec.len()) { - | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is only used to index `vec`. - --> $DIR/for_loop.rs:75:14 - | -LL | for i in 5..vec.len() { - | ^^^^^^^^^^^^ -help: consider using an iterator - | -LL | for in vec.iter().skip(5) { - | ^^^^^^ ^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is only used to index `vec`. - --> $DIR/for_loop.rs:79:14 - | -LL | for i in 0..MAX_LEN { - | ^^^^^^^^^^ -help: consider using an iterator - | -LL | for in vec.iter().take(MAX_LEN) { - | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is only used to index `vec`. - --> $DIR/for_loop.rs:83:14 - | -LL | for i in 0..=MAX_LEN { - | ^^^^^^^^^^^ -help: consider using an iterator - | -LL | for in vec.iter().take(MAX_LEN + 1) { - | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is only used to index `vec`. - --> $DIR/for_loop.rs:87:14 - | -LL | for i in 5..10 { - | ^^^^^ -help: consider using an iterator - | -LL | for in vec.iter().take(10).skip(5) { - | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is only used to index `vec`. - --> $DIR/for_loop.rs:91:14 - | -LL | for i in 5..=10 { - | ^^^^^^ -help: consider using an iterator - | -LL | for in vec.iter().take(10 + 1).skip(5) { - | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is used to index `vec` - --> $DIR/for_loop.rs:95:14 - | -LL | for i in 5..vec.len() { - | ^^^^^^^^^^^^ -help: consider using an iterator - | -LL | for (i, ) in vec.iter().enumerate().skip(5) { - | ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: the loop variable `i` is used to index `vec` - --> $DIR/for_loop.rs:99:14 - | -LL | for i in 5..10 { - | ^^^^^ -help: consider using an iterator - | -LL | for (i, ) in vec.iter().enumerate().take(10).skip(5) { - | ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - error: this range is empty so this for loop will never run - --> $DIR/for_loop.rs:103:14 + --> $DIR/for_loop.rs:40:14 | LL | for i in 10..0 { | ^^^^^ @@ -143,7 +11,7 @@ LL | for i in (0..10).rev() { | ^^^^^^^^^^^^^ error: this range is empty so this for loop will never run - --> $DIR/for_loop.rs:107:14 + --> $DIR/for_loop.rs:44:14 | LL | for i in 10..=0 { | ^^^^^^ @@ -153,7 +21,7 @@ LL | for i in (0...10).rev() { | ^^^^^^^^^^^^^^ error: this range is empty so this for loop will never run - --> $DIR/for_loop.rs:111:14 + --> $DIR/for_loop.rs:48:14 | LL | for i in MAX_LEN..0 { | ^^^^^^^^^^ @@ -163,13 +31,13 @@ LL | for i in (0..MAX_LEN).rev() { | ^^^^^^^^^^^^^^^^^^ error: this range is empty so this for loop will never run - --> $DIR/for_loop.rs:115:14 + --> $DIR/for_loop.rs:52:14 | LL | for i in 5..5 { | ^^^^ error: this range is empty so this for loop will never run - --> $DIR/for_loop.rs:140:14 + --> $DIR/for_loop.rs:77:14 | LL | for i in 10..5 + 4 { | ^^^^^^^^^ @@ -179,7 +47,7 @@ LL | for i in (5 + 4..10).rev() { | ^^^^^^^^^^^^^^^^^ error: this range is empty so this for loop will never run - --> $DIR/for_loop.rs:144:14 + --> $DIR/for_loop.rs:81:14 | LL | for i in (5 + 2)..(3 - 1) { | ^^^^^^^^^^^^^^^^ @@ -189,13 +57,13 @@ LL | for i in ((3 - 1)..(5 + 2)).rev() { | ^^^^^^^^^^^^^^^^^^^^^^^^ error: this range is empty so this for loop will never run - --> $DIR/for_loop.rs:148:14 + --> $DIR/for_loop.rs:85:14 | LL | for i in (5 + 2)..(8 - 1) { | ^^^^^^^^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:170:15 + --> $DIR/for_loop.rs:107:15 | LL | for _v in vec.iter() {} | ^^^^^^^^^^ @@ -203,13 +71,13 @@ LL | for _v in vec.iter() {} = note: `-D clippy::explicit-iter-loop` implied by `-D warnings` error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:172:15 + --> $DIR/for_loop.rs:109:15 | LL | for _v in vec.iter_mut() {} | ^^^^^^^^^^^^^^ error: it is more concise to loop over containers instead of using explicit iteration methods` - --> $DIR/for_loop.rs:175:15 + --> $DIR/for_loop.rs:112:15 | LL | for _v in out_vec.into_iter() {} | ^^^^^^^^^^^^^^^^^^^ @@ -217,67 +85,67 @@ LL | for _v in out_vec.into_iter() {} = note: `-D clippy::explicit-into-iter-loop` implied by `-D warnings` error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:178:15 + --> $DIR/for_loop.rs:115:15 | LL | for _v in array.into_iter() {} | ^^^^^^^^^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:183:15 + --> $DIR/for_loop.rs:120:15 | LL | for _v in [1, 2, 3].iter() {} | ^^^^^^^^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:187:15 + --> $DIR/for_loop.rs:124:15 | LL | for _v in [0; 32].iter() {} | ^^^^^^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:192:15 + --> $DIR/for_loop.rs:129:15 | LL | for _v in ll.iter() {} | ^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:195:15 + --> $DIR/for_loop.rs:132:15 | LL | for _v in vd.iter() {} | ^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:198:15 + --> $DIR/for_loop.rs:135:15 | LL | for _v in bh.iter() {} | ^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:201:15 + --> $DIR/for_loop.rs:138:15 | LL | for _v in hm.iter() {} | ^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:204:15 + --> $DIR/for_loop.rs:141:15 | LL | for _v in bt.iter() {} | ^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:207:15 + --> $DIR/for_loop.rs:144:15 | LL | for _v in hs.iter() {} | ^^^^^^^^^ error: it is more concise to loop over references to containers instead of using explicit iteration methods - --> $DIR/for_loop.rs:210:15 + --> $DIR/for_loop.rs:147:15 | LL | for _v in bs.iter() {} | ^^^^^^^^^ error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want - --> $DIR/for_loop.rs:212:15 + --> $DIR/for_loop.rs:149:15 | LL | for _v in vec.iter().next() {} | ^^^^^^^^^^^^^^^^^ @@ -285,12 +153,12 @@ LL | for _v in vec.iter().next() {} = note: `-D clippy::iter-next-loop` implied by `-D warnings` error: you are collect()ing an iterator and throwing away the result. Consider using an explicit for loop to exhaust the iterator - --> $DIR/for_loop.rs:219:5 + --> $DIR/for_loop.rs:156:5 | LL | vec.iter().cloned().map(|x| out.push(x)).collect::>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::unused-collect` implied by `-D warnings` -error: aborting due to 35 previous errors +error: aborting due to 22 previous errors diff --git a/tests/ui/needless_range_loop.rs b/tests/ui/needless_range_loop.rs index 70ad6eac65f..6f1f4c5fb04 100644 --- a/tests/ui/needless_range_loop.rs +++ b/tests/ui/needless_range_loop.rs @@ -1,8 +1,15 @@ +#![allow(clippy::cognitive_complexity)] + +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 } fn main() { + const MAX_LEN: usize = 42; + let ns = vec![2, 3, 5, 7]; for i in 3..10 { @@ -81,6 +88,70 @@ fn main() { println!("{}", arr[i]); } + let mut vec = vec![1, 2, 3, 4]; + let vec2 = vec![1, 2, 3, 4]; + for i in 0..vec.len() { + println!("{}", vec[i]); + } + + for i in 0..vec.len() { + let i = 42; // make a different `i` + println!("{}", vec[i]); // ok, not the `i` of the for-loop + } + + for i in 0..vec.len() { + let _ = vec[i]; + } + + // ICE #746 + for j in 0..4 { + println!("{:?}", STATIC[j]); + } + + for j in 0..4 { + println!("{:?}", CONST[j]); + } + + for i in 0..vec.len() { + println!("{} {}", vec[i], i); + } + for i in 0..vec.len() { + // not an error, indexing more than one variable + println!("{} {}", vec[i], vec2[i]); + } + + for i in 0..vec.len() { + println!("{}", vec2[i]); + } + + for i in 5..vec.len() { + println!("{}", vec[i]); + } + + for i in 0..MAX_LEN { + println!("{}", vec[i]); + } + + for i in 0..=MAX_LEN { + println!("{}", vec[i]); + } + + for i in 5..10 { + println!("{}", vec[i]); + } + + for i in 5..=10 { + println!("{}", vec[i]); + } + + for i in 5..vec.len() { + println!("{} {}", vec[i], i); + } + + for i in 5..10 { + println!("{} {}", vec[i], i); + } + // #2542 for i in 0..vec.len() { vec[i] = Some(1).unwrap_or_else(|| panic!("error on {}", i)); diff --git a/tests/ui/needless_range_loop.stderr b/tests/ui/needless_range_loop.stderr index d1cc9b3ce72..4df07c6f1e5 100644 --- a/tests/ui/needless_range_loop.stderr +++ b/tests/ui/needless_range_loop.stderr @@ -1,5 +1,5 @@ error: the loop variable `i` is only used to index `ns`. - --> $DIR/needless_range_loop.rs:8:14 + --> $DIR/needless_range_loop.rs:15:14 | LL | for i in 3..10 { | ^^^^^ @@ -11,7 +11,7 @@ LL | for in ns.iter().take(10).skip(3) { | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: the loop variable `i` is only used to index `ms`. - --> $DIR/needless_range_loop.rs:29:14 + --> $DIR/needless_range_loop.rs:36:14 | LL | for i in 0..ms.len() { | ^^^^^^^^^^^ @@ -21,7 +21,7 @@ LL | for in &mut ms { | ^^^^^^ ^^^^^^^ error: the loop variable `i` is only used to index `ms`. - --> $DIR/needless_range_loop.rs:35:14 + --> $DIR/needless_range_loop.rs:42:14 | LL | for i in 0..ms.len() { | ^^^^^^^^^^^ @@ -31,7 +31,7 @@ LL | for in &mut ms { | ^^^^^^ ^^^^^^^ error: the loop variable `i` is only used to index `vec`. - --> $DIR/needless_range_loop.rs:59:14 + --> $DIR/needless_range_loop.rs:66:14 | LL | for i in x..x + 4 { | ^^^^^^^^ @@ -41,7 +41,7 @@ LL | for in vec.iter_mut().skip(x).take(4) { | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: the loop variable `i` is only used to index `vec`. - --> $DIR/needless_range_loop.rs:66:14 + --> $DIR/needless_range_loop.rs:73:14 | LL | for i in x..=x + 4 { | ^^^^^^^^^ @@ -51,7 +51,7 @@ LL | for 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:72:14 + --> $DIR/needless_range_loop.rs:79:14 | LL | for i in 0..3 { | ^^^^ @@ -61,7 +61,7 @@ LL | for in &arr { | ^^^^^^ ^^^^ error: the loop variable `i` is only used to index `arr`. - --> $DIR/needless_range_loop.rs:76:14 + --> $DIR/needless_range_loop.rs:83:14 | LL | for i in 0..2 { | ^^^^ @@ -71,7 +71,7 @@ LL | for in arr.iter().take(2) { | ^^^^^^ ^^^^^^^^^^^^^^^^^^ error: the loop variable `i` is only used to index `arr`. - --> $DIR/needless_range_loop.rs:80:14 + --> $DIR/needless_range_loop.rs:87:14 | LL | for i in 1..3 { | ^^^^ @@ -80,8 +80,138 @@ help: consider using an iterator LL | for in arr.iter().skip(1) { | ^^^^^^ ^^^^^^^^^^^^^^^^^^ +error: the loop variable `i` is only used to index `vec`. + --> $DIR/needless_range_loop.rs:93:14 + | +LL | for i in 0..vec.len() { + | ^^^^^^^^^^^^ +help: consider using an iterator + | +LL | for in &vec { + | ^^^^^^ ^^^^ + +error: the loop variable `i` is only used to index `vec`. + --> $DIR/needless_range_loop.rs:102:14 + | +LL | for i in 0..vec.len() { + | ^^^^^^^^^^^^ +help: consider using an iterator + | +LL | for in &vec { + | ^^^^^^ ^^^^ + +error: the loop variable `j` is only used to index `STATIC`. + --> $DIR/needless_range_loop.rs:107:14 + | +LL | for j in 0..4 { + | ^^^^ +help: consider using an iterator + | +LL | for in &STATIC { + | ^^^^^^ ^^^^^^^ + +error: the loop variable `j` is only used to index `CONST`. + --> $DIR/needless_range_loop.rs:111:14 + | +LL | for j in 0..4 { + | ^^^^ +help: consider using an iterator + | +LL | for in &CONST { + | ^^^^^^ ^^^^^^ + error: the loop variable `i` is used to index `vec` - --> $DIR/needless_range_loop.rs:85:14 + --> $DIR/needless_range_loop.rs:115:14 + | +LL | for i in 0..vec.len() { + | ^^^^^^^^^^^^ +help: consider using an iterator + | +LL | for (i, ) in vec.iter().enumerate() { + | ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ + +error: the loop variable `i` is only used to index `vec2`. + --> $DIR/needless_range_loop.rs:123:14 + | +LL | for i in 0..vec.len() { + | ^^^^^^^^^^^^ +help: consider using an iterator + | +LL | for in vec2.iter().take(vec.len()) { + | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the loop variable `i` is only used to index `vec`. + --> $DIR/needless_range_loop.rs:127:14 + | +LL | for i in 5..vec.len() { + | ^^^^^^^^^^^^ +help: consider using an iterator + | +LL | for in vec.iter().skip(5) { + | ^^^^^^ ^^^^^^^^^^^^^^^^^^ + +error: the loop variable `i` is only used to index `vec`. + --> $DIR/needless_range_loop.rs:131:14 + | +LL | for i in 0..MAX_LEN { + | ^^^^^^^^^^ +help: consider using an iterator + | +LL | for in vec.iter().take(MAX_LEN) { + | ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the loop variable `i` is only used to index `vec`. + --> $DIR/needless_range_loop.rs:135:14 + | +LL | for i in 0..=MAX_LEN { + | ^^^^^^^^^^^ +help: consider using an iterator + | +LL | for 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 + | +LL | for i in 5..10 { + | ^^^^^ +help: consider using an iterator + | +LL | for 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 + | +LL | for i in 5..=10 { + | ^^^^^^ +help: consider using an iterator + | +LL | for 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 + | +LL | for i in 5..vec.len() { + | ^^^^^^^^^^^^ +help: consider using an iterator + | +LL | for (i, ) in vec.iter().enumerate().skip(5) { + | ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: the loop variable `i` is used to index `vec` + --> $DIR/needless_range_loop.rs:151:14 + | +LL | for i in 5..10 { + | ^^^^^ +help: consider using an iterator + | +LL | for (i, ) 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 | LL | for i in 0..vec.len() { | ^^^^^^^^^^^^ @@ -90,5 +220,5 @@ help: consider using an iterator LL | for (i, ) in vec.iter_mut().enumerate() { | ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 9 previous errors +error: aborting due to 22 previous errors