]> git.lizzy.rs Git - rust.git/blob - tests/ui/for_loop_over_option_result.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / for_loop_over_option_result.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::for_loop_over_option, clippy::for_loop_over_result)]
11
12 /// Tests for_loop_over_result and for_loop_over_option
13
14 fn for_loop_over_option_and_result() {
15     let option = Some(1);
16     let result = option.ok_or("x not found");
17     let v = vec![0, 1, 2];
18
19     // check FOR_LOOP_OVER_OPTION lint
20     for x in option {
21         println!("{}", x);
22     }
23
24     // check FOR_LOOP_OVER_RESULT lint
25     for x in result {
26         println!("{}", x);
27     }
28
29     for x in option.ok_or("x not found") {
30         println!("{}", x);
31     }
32
33     // make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
34     // in the chain
35     for x in v.iter().next() {
36         println!("{}", x);
37     }
38
39     // make sure we lint when next() is not the last call in the chain
40     for x in v.iter().next().and(Some(0)) {
41         println!("{}", x);
42     }
43
44     for x in v.iter().next().ok_or("x not found") {
45         println!("{}", x);
46     }
47
48     // check for false positives
49
50     // for loop false positive
51     for x in v {
52         println!("{}", x);
53     }
54
55     // while let false positive for Option
56     while let Some(x) = option {
57         println!("{}", x);
58         break;
59     }
60
61     // while let false positive for Result
62     while let Ok(x) = result {
63         println!("{}", x);
64         break;
65     }
66 }
67
68 fn main() {}