]> git.lizzy.rs Git - rust.git/blob - tests/ui/for_loop_over_option_result.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / tests / ui / for_loop_over_option_result.rs
1 #![warn(clippy::for_loop_over_option, clippy::for_loop_over_result)]
2
3 /// Tests for_loop_over_result and for_loop_over_option
4
5 fn for_loop_over_option_and_result() {
6     let option = Some(1);
7     let result = option.ok_or("x not found");
8     let v = vec![0, 1, 2];
9
10     // check FOR_LOOP_OVER_OPTION lint
11     for x in option {
12         println!("{}", x);
13     }
14
15     // check FOR_LOOP_OVER_RESULT lint
16     for x in result {
17         println!("{}", x);
18     }
19
20     for x in option.ok_or("x not found") {
21         println!("{}", x);
22     }
23
24     // make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
25     // in the chain
26     for x in v.iter().next() {
27         println!("{}", x);
28     }
29
30     // make sure we lint when next() is not the last call in the chain
31     for x in v.iter().next().and(Some(0)) {
32         println!("{}", x);
33     }
34
35     for x in v.iter().next().ok_or("x not found") {
36         println!("{}", x);
37     }
38
39     // check for false positives
40
41     // for loop false positive
42     for x in v {
43         println!("{}", x);
44     }
45
46     // while let false positive for Option
47     while let Some(x) = option {
48         println!("{}", x);
49         break;
50     }
51
52     // while let false positive for Result
53     while let Ok(x) = result {
54         println!("{}", x);
55         break;
56     }
57 }
58
59 fn main() {}