]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_flatten.rs
Auto merge of #85538 - r00ster91:iterrepeat, r=Mark-Simulacrum
[rust.git] / tests / ui / manual_flatten.rs
1 #![warn(clippy::manual_flatten)]
2 #![allow(clippy::useless_vec)]
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 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     // Note: if `clippy::manual_flatten` is made autofixable, this case will
30     // lead to a follow-up lint `clippy::into_iter_on_ref`
31     let z = &y;
32     for n in z {
33         if let Ok(n) = n {
34             println!("{}", n);
35         }
36     }
37
38     // Test for loop over `Iterator` with `if let` expression
39     let z = vec![Some(1), Some(2), Some(3)];
40     let z = z.iter();
41     for n in z {
42         if let Some(m) = n {
43             println!("{}", m);
44         }
45     }
46
47     // Using the `None` variant should not trigger the lint
48     // Note: for an autofixable suggestion, the binding in the for loop has to take the
49     // name of the binding in the `if let`
50     let z = vec![Some(1), Some(2), Some(3)];
51     for n in z {
52         if n.is_none() {
53             println!("Nada.");
54         }
55     }
56
57     // Using the `Err` variant should not trigger the lint
58     for n in y.clone() {
59         if let Err(e) = n {
60             println!("Oops: {}!", e);
61         }
62     }
63
64     // Having an else clause should not trigger the lint
65     for n in y.clone() {
66         if let Ok(n) = n {
67             println!("{}", n);
68         } else {
69             println!("Oops!");
70         }
71     }
72
73     let vec_of_ref = vec![&Some(1)];
74     for n in &vec_of_ref {
75         if let Some(n) = n {
76             println!("{:?}", n);
77         }
78     }
79
80     let vec_of_ref = &vec_of_ref;
81     for n in vec_of_ref {
82         if let Some(n) = n {
83             println!("{:?}", n);
84         }
85     }
86
87     let slice_of_ref = &[&Some(1)];
88     for n in slice_of_ref {
89         if let Some(n) = n {
90             println!("{:?}", n);
91         }
92     }
93
94     // Using manual flatten should not trigger the lint
95     for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
96         println!("{}", n);
97     }
98 }