Skip to content

Commit 5128140

Browse files
committed
Add a test for the for_loop_over_fallibles lint
1 parent 2bf213b commit 5128140

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// check-pass
2+
3+
fn main() {
4+
// Common
5+
for _ in Some(1) {}
6+
//~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement
7+
//~| HELP to check pattern in a loop use `while let`
8+
//~| HELP consider using `if let` to clear intent
9+
for _ in Ok::<_, ()>(1) {}
10+
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
11+
//~| HELP to check pattern in a loop use `while let`
12+
//~| HELP consider using `if let` to clear intent
13+
14+
// `Iterator::next` specific
15+
for _ in [0; 0].iter().next() {}
16+
//~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement
17+
//~| HELP to iterate over `[0; 0].iter()` remove the call to `next`
18+
//~| HELP consider using `if let` to clear intent
19+
20+
// `Result<impl Iterator, _>`, but function doesn't return `Result`
21+
for _ in Ok::<_, ()>([0; 0].iter()) {}
22+
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
23+
//~| HELP to check pattern in a loop use `while let`
24+
//~| HELP consider using `if let` to clear intent
25+
}
26+
27+
fn _returns_result() -> Result<(), ()> {
28+
// `Result<impl Iterator, _>`
29+
for _ in Ok::<_, ()>([0; 0].iter()) {}
30+
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
31+
//~| HELP to check pattern in a loop use `while let`
32+
//~| HELP consider unwrapping the `Result` with `?` to iterate over its contents
33+
//~| HELP consider using `if let` to clear intent
34+
35+
// `Result<impl IntoIterator>`
36+
for _ in Ok::<_, ()>([0; 0]) {}
37+
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
38+
//~| HELP to check pattern in a loop use `while let`
39+
//~| HELP consider unwrapping the `Result` with `?` to iterate over its contents
40+
//~| HELP consider using `if let` to clear intent
41+
42+
Ok(())
43+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
warning: for loop over an `Option`. This is more readably written as an `if let` statement
2+
--> $DIR/for_loop_over_fallibles.rs:5:14
3+
|
4+
LL | for _ in Some(1) {}
5+
| ^^^^^^^
6+
|
7+
= note: `#[warn(for_loop_over_fallibles)]` on by default
8+
help: to check pattern in a loop use `while let`
9+
|
10+
LL | while let Some(_) = Some(1) {}
11+
| ~~~~~~~~~~~~~~~ ~~~
12+
help: consider using `if let` to clear intent
13+
|
14+
LL | if let Some(_) = Some(1) {}
15+
| ~~~~~~~~~~~~ ~~~
16+
17+
warning: for loop over a `Result`. This is more readably written as an `if let` statement
18+
--> $DIR/for_loop_over_fallibles.rs:9:14
19+
|
20+
LL | for _ in Ok::<_, ()>(1) {}
21+
| ^^^^^^^^^^^^^^
22+
|
23+
help: to check pattern in a loop use `while let`
24+
|
25+
LL | while let Ok(_) = Ok::<_, ()>(1) {}
26+
| ~~~~~~~~~~~~~ ~~~
27+
help: consider using `if let` to clear intent
28+
|
29+
LL | if let Ok(_) = Ok::<_, ()>(1) {}
30+
| ~~~~~~~~~~ ~~~
31+
32+
warning: for loop over an `Option`. This is more readably written as an `if let` statement
33+
--> $DIR/for_loop_over_fallibles.rs:15:14
34+
|
35+
LL | for _ in [0; 0].iter().next() {}
36+
| ^^^^^^^^^^^^^^^^^^^^
37+
|
38+
help: to iterate over `[0; 0].iter()` remove the call to `next`
39+
|
40+
LL - for _ in [0; 0].iter().next() {}
41+
LL + for _ in [0; 0].iter() {}
42+
|
43+
help: consider using `if let` to clear intent
44+
|
45+
LL | if let Some(_) = [0; 0].iter().next() {}
46+
| ~~~~~~~~~~~~ ~~~
47+
48+
warning: for loop over a `Result`. This is more readably written as an `if let` statement
49+
--> $DIR/for_loop_over_fallibles.rs:21:14
50+
|
51+
LL | for _ in Ok::<_, ()>([0; 0].iter()) {}
52+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
53+
|
54+
help: to check pattern in a loop use `while let`
55+
|
56+
LL | while let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
57+
| ~~~~~~~~~~~~~ ~~~
58+
help: consider using `if let` to clear intent
59+
|
60+
LL | if let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
61+
| ~~~~~~~~~~ ~~~
62+
63+
warning: for loop over a `Result`. This is more readably written as an `if let` statement
64+
--> $DIR/for_loop_over_fallibles.rs:29:14
65+
|
66+
LL | for _ in Ok::<_, ()>([0; 0].iter()) {}
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
|
69+
help: to check pattern in a loop use `while let`
70+
|
71+
LL | while let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
72+
| ~~~~~~~~~~~~~ ~~~
73+
help: consider unwrapping the `Result` with `?` to iterate over its contents
74+
|
75+
LL | for _ in Ok::<_, ()>([0; 0].iter())? {}
76+
| +
77+
help: consider using `if let` to clear intent
78+
|
79+
LL | if let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
80+
| ~~~~~~~~~~ ~~~
81+
82+
warning: for loop over a `Result`. This is more readably written as an `if let` statement
83+
--> $DIR/for_loop_over_fallibles.rs:36:14
84+
|
85+
LL | for _ in Ok::<_, ()>([0; 0]) {}
86+
| ^^^^^^^^^^^^^^^^^^^
87+
|
88+
help: to check pattern in a loop use `while let`
89+
|
90+
LL | while let Ok(_) = Ok::<_, ()>([0; 0]) {}
91+
| ~~~~~~~~~~~~~ ~~~
92+
help: consider unwrapping the `Result` with `?` to iterate over its contents
93+
|
94+
LL | for _ in Ok::<_, ()>([0; 0])? {}
95+
| +
96+
help: consider using `if let` to clear intent
97+
|
98+
LL | if let Ok(_) = Ok::<_, ()>([0; 0]) {}
99+
| ~~~~~~~~~~ ~~~
100+
101+
warning: 6 warnings emitted
102+

0 commit comments

Comments
 (0)