Skip to content

Commit 064431d

Browse files
committed
Re-add old tests for empty range loops
1 parent 0f2b119 commit 064431d

5 files changed

+210
-0
lines changed

Diff for: tests/ui/reversed_empty_ranges_loops_fixable.fixed

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// run-rustfix
2+
#![warn(clippy::reversed_empty_ranges)]
3+
4+
fn main() {
5+
const MAX_LEN: usize = 42;
6+
7+
for i in (0..10).rev() {
8+
println!("{}", i);
9+
}
10+
11+
for i in (0..=10).rev() {
12+
println!("{}", i);
13+
}
14+
15+
for i in (0..MAX_LEN).rev() {
16+
println!("{}", i);
17+
}
18+
19+
for i in 5..=5 {
20+
// not an error, this is the range with only one element “5”
21+
println!("{}", i);
22+
}
23+
24+
for i in 0..10 {
25+
// not an error, the start index is less than the end index
26+
println!("{}", i);
27+
}
28+
29+
for i in -10..0 {
30+
// not an error
31+
println!("{}", i);
32+
}
33+
34+
for i in (0..10).rev().map(|x| x * 2) {
35+
println!("{}", i);
36+
}
37+
38+
// testing that the empty range lint folds constants
39+
for i in (5 + 4..10).rev() {
40+
println!("{}", i);
41+
}
42+
43+
for i in ((3 - 1)..(5 + 2)).rev() {
44+
println!("{}", i);
45+
}
46+
47+
for i in (2 * 2)..(2 * 3) {
48+
// no error, 4..6 is fine
49+
println!("{}", i);
50+
}
51+
52+
let x = 42;
53+
for i in x..10 {
54+
// no error, not constant-foldable
55+
println!("{}", i);
56+
}
57+
}

Diff for: tests/ui/reversed_empty_ranges_loops_fixable.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// run-rustfix
2+
#![warn(clippy::reversed_empty_ranges)]
3+
4+
fn main() {
5+
const MAX_LEN: usize = 42;
6+
7+
for i in 10..0 {
8+
println!("{}", i);
9+
}
10+
11+
for i in 10..=0 {
12+
println!("{}", i);
13+
}
14+
15+
for i in MAX_LEN..0 {
16+
println!("{}", i);
17+
}
18+
19+
for i in 5..=5 {
20+
// not an error, this is the range with only one element “5”
21+
println!("{}", i);
22+
}
23+
24+
for i in 0..10 {
25+
// not an error, the start index is less than the end index
26+
println!("{}", i);
27+
}
28+
29+
for i in -10..0 {
30+
// not an error
31+
println!("{}", i);
32+
}
33+
34+
for i in (10..0).map(|x| x * 2) {
35+
println!("{}", i);
36+
}
37+
38+
// testing that the empty range lint folds constants
39+
for i in 10..5 + 4 {
40+
println!("{}", i);
41+
}
42+
43+
for i in (5 + 2)..(3 - 1) {
44+
println!("{}", i);
45+
}
46+
47+
for i in (2 * 2)..(2 * 3) {
48+
// no error, 4..6 is fine
49+
println!("{}", i);
50+
}
51+
52+
let x = 42;
53+
for i in x..10 {
54+
// no error, not constant-foldable
55+
println!("{}", i);
56+
}
57+
}

Diff for: tests/ui/reversed_empty_ranges_loops_fixable.stderr

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
error: this range is empty so it will yield no values
2+
--> $DIR/reversed_empty_ranges_loops_fixable.rs:7:14
3+
|
4+
LL | for i in 10..0 {
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::reversed-empty-ranges` implied by `-D warnings`
8+
help: consider using the following if you are attempting to iterate over this range in reverse
9+
|
10+
LL | for i in (0..10).rev() {
11+
| ^^^^^^^^^^^^^
12+
13+
error: this range is empty so it will yield no values
14+
--> $DIR/reversed_empty_ranges_loops_fixable.rs:11:14
15+
|
16+
LL | for i in 10..=0 {
17+
| ^^^^^^
18+
|
19+
help: consider using the following if you are attempting to iterate over this range in reverse
20+
|
21+
LL | for i in (0..=10).rev() {
22+
| ^^^^^^^^^^^^^^
23+
24+
error: this range is empty so it will yield no values
25+
--> $DIR/reversed_empty_ranges_loops_fixable.rs:15:14
26+
|
27+
LL | for i in MAX_LEN..0 {
28+
| ^^^^^^^^^^
29+
|
30+
help: consider using the following if you are attempting to iterate over this range in reverse
31+
|
32+
LL | for i in (0..MAX_LEN).rev() {
33+
| ^^^^^^^^^^^^^^^^^^
34+
35+
error: this range is empty so it will yield no values
36+
--> $DIR/reversed_empty_ranges_loops_fixable.rs:34:14
37+
|
38+
LL | for i in (10..0).map(|x| x * 2) {
39+
| ^^^^^^^
40+
|
41+
help: consider using the following if you are attempting to iterate over this range in reverse
42+
|
43+
LL | for i in (0..10).rev().map(|x| x * 2) {
44+
| ^^^^^^^^^^^^^
45+
46+
error: this range is empty so it will yield no values
47+
--> $DIR/reversed_empty_ranges_loops_fixable.rs:39:14
48+
|
49+
LL | for i in 10..5 + 4 {
50+
| ^^^^^^^^^
51+
|
52+
help: consider using the following if you are attempting to iterate over this range in reverse
53+
|
54+
LL | for i in (5 + 4..10).rev() {
55+
| ^^^^^^^^^^^^^^^^^
56+
57+
error: this range is empty so it will yield no values
58+
--> $DIR/reversed_empty_ranges_loops_fixable.rs:43:14
59+
|
60+
LL | for i in (5 + 2)..(3 - 1) {
61+
| ^^^^^^^^^^^^^^^^
62+
|
63+
help: consider using the following if you are attempting to iterate over this range in reverse
64+
|
65+
LL | for i in ((3 - 1)..(5 + 2)).rev() {
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^
67+
68+
error: aborting due to 6 previous errors
69+

Diff for: tests/ui/reversed_empty_ranges_loops_unfixable.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![warn(clippy::reversed_empty_ranges)]
2+
3+
fn main() {
4+
for i in 5..5 {
5+
println!("{}", i);
6+
}
7+
8+
for i in (5 + 2)..(8 - 1) {
9+
println!("{}", i);
10+
}
11+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: this range is empty so it will yield no values
2+
--> $DIR/reversed_empty_ranges_loops_unfixable.rs:4:14
3+
|
4+
LL | for i in 5..5 {
5+
| ^^^^
6+
|
7+
= note: `-D clippy::reversed-empty-ranges` implied by `-D warnings`
8+
9+
error: this range is empty so it will yield no values
10+
--> $DIR/reversed_empty_ranges_loops_unfixable.rs:8:14
11+
|
12+
LL | for i in (5 + 2)..(8 - 1) {
13+
| ^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)