]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_flatten.rs
fix dupe word typos
[rust.git] / tests / ui / manual_flatten.rs
1 #![warn(clippy::manual_flatten)]
2 #![allow(clippy::useless_vec, clippy::uninlined_format_args)]
3
4 fn main() {
5     // Test for loop over implicitly adjusted `Iterator` with `if let` expression
6     let x = vec![Some(1), Some(2), Some(3)];
7     for n in x {
8         if let Some(y) = n {
9             println!("{}", y);
10         }
11     }
12
13     // Test for loop over implicitly adjusted `Iterator` with `if let` statement
14     let y: Vec<Result<i32, i32>> = vec![];
15     for n in y.clone() {
16         if let Ok(n) = n {
17             println!("{}", n);
18         };
19     }
20
21     // Test for loop over by reference
22     for n in &y {
23         if let Ok(n) = n {
24             println!("{}", n);
25         }
26     }
27
28     // Test for loop over an implicit reference
29     let z = &y;
30     for n in z {
31         if let Ok(n) = n {
32             println!("{}", n);
33         }
34     }
35
36     // Test for loop over `Iterator` with `if let` expression
37     let z = vec![Some(1), Some(2), Some(3)];
38     let z = z.iter();
39     for n in z {
40         if let Some(m) = n {
41             println!("{}", m);
42         }
43     }
44
45     // Using the `None` variant should not trigger the lint
46     // Note: for an autofixable suggestion, the binding in the for loop has to take the
47     // name of the binding in the `if let`
48     let z = vec![Some(1), Some(2), Some(3)];
49     for n in z {
50         if n.is_none() {
51             println!("Nada.");
52         }
53     }
54
55     // Using the `Err` variant should not trigger the lint
56     for n in y.clone() {
57         if let Err(e) = n {
58             println!("Oops: {}!", e);
59         }
60     }
61
62     // Having an else clause should not trigger the lint
63     for n in y.clone() {
64         if let Ok(n) = n {
65             println!("{}", n);
66         } else {
67             println!("Oops!");
68         }
69     }
70
71     let vec_of_ref = vec![&Some(1)];
72     for n in &vec_of_ref {
73         if let Some(n) = n {
74             println!("{:?}", n);
75         }
76     }
77
78     let vec_of_ref = &vec_of_ref;
79     for n in vec_of_ref {
80         if let Some(n) = n {
81             println!("{:?}", n);
82         }
83     }
84
85     let slice_of_ref = &[&Some(1)];
86     for n in slice_of_ref {
87         if let Some(n) = n {
88             println!("{:?}", n);
89         }
90     }
91
92     struct Test {
93         a: usize,
94     }
95
96     let mut vec_of_struct = [Some(Test { a: 1 }), None];
97
98     // Usage of `if let` expression should not trigger lint
99     for n in vec_of_struct.iter_mut() {
100         if let Some(z) = n {
101             *n = None;
102         }
103     }
104
105     // Using manual flatten should not trigger the lint
106     for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
107         println!("{}", n);
108     }
109
110     run_unformatted_tests();
111 }
112
113 #[rustfmt::skip]
114 fn run_unformatted_tests() {
115     // Skip rustfmt here on purpose so the suggestion does not fit in one line
116     for n in vec![
117         Some(1),
118         Some(2),
119         Some(3)
120     ].iter() {
121         if let Some(n) = n {
122             println!("{:?}", n);
123         }
124     }
125 }