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