]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/for_loops_over_fallibles.rs
Auto merge of #101969 - reez12g:issue-101306, r=reez12g
[rust.git] / src / tools / clippy / tests / ui / for_loops_over_fallibles.rs
1 #![warn(clippy::for_loops_over_fallibles)]
2
3 fn for_loops_over_fallibles() {
4     let option = Some(1);
5     let mut result = option.ok_or("x not found");
6     let v = vec![0, 1, 2];
7
8     // check over an `Option`
9     for x in option {
10         println!("{}", x);
11     }
12
13     // check over an `Option`
14     for x in option.iter() {
15         println!("{}", x);
16     }
17
18     // check over a `Result`
19     for x in result {
20         println!("{}", x);
21     }
22
23     // check over a `Result`
24     for x in result.iter_mut() {
25         println!("{}", x);
26     }
27
28     // check over a `Result`
29     for x in result.into_iter() {
30         println!("{}", x);
31     }
32
33     for x in option.ok_or("x not found") {
34         println!("{}", x);
35     }
36
37     // make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
38     // in the chain
39     for x in v.iter().next() {
40         println!("{}", x);
41     }
42
43     // make sure we lint when next() is not the last call in the chain
44     for x in v.iter().next().and(Some(0)) {
45         println!("{}", x);
46     }
47
48     for x in v.iter().next().ok_or("x not found") {
49         println!("{}", x);
50     }
51
52     // check for false positives
53
54     // for loop false positive
55     for x in v {
56         println!("{}", x);
57     }
58
59     // while let false positive for Option
60     while let Some(x) = option {
61         println!("{}", x);
62         break;
63     }
64
65     // while let false positive for Result
66     while let Ok(x) = result {
67         println!("{}", x);
68         break;
69     }
70 }
71
72 fn main() {}