]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/for_loops_over_fallibles.rs
Merge branch 'master' into hooks
[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 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 a `Result`
14     for x in result {
15         println!("{}", x);
16     }
17
18     for x in option.ok_or("x not found") {
19         println!("{}", x);
20     }
21
22     // make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
23     // in the chain
24     for x in v.iter().next() {
25         println!("{}", x);
26     }
27
28     // make sure we lint when next() is not the last call in the chain
29     for x in v.iter().next().and(Some(0)) {
30         println!("{}", x);
31     }
32
33     for x in v.iter().next().ok_or("x not found") {
34         println!("{}", x);
35     }
36
37     // check for false positives
38
39     // for loop false positive
40     for x in v {
41         println!("{}", x);
42     }
43
44     // while let false positive for Option
45     while let Some(x) = option {
46         println!("{}", x);
47         break;
48     }
49
50     // while let false positive for Result
51     while let Ok(x) = result {
52         println!("{}", x);
53         break;
54     }
55 }
56
57 fn main() {}