1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#![warn(clippy::manual_flatten)]
#![allow(clippy::useless_vec, clippy::uninlined_format_args)]
fn main() {
// Test for loop over implicitly adjusted `Iterator` with `if let` expression
let x = vec![Some(1), Some(2), Some(3)];
for y in x.into_iter().flatten() {
println!("{}", y);
}
// Test for loop over implicitly adjusted `Iterator` with `if let` statement
let y: Vec<Result<i32, i32>> = vec![];
for n in y.clone().into_iter().flatten() {
println!("{}", n);
}
// Test for loop over by reference
for n in y.iter().flatten() {
println!("{}", n);
}
// Test for loop over an implicit reference
let z = &y;
for n in z.iter().flatten() {
println!("{}", n);
}
// Test for loop over `Iterator` with `if let` expression
let z = vec![Some(1), Some(2), Some(3)];
let z = z.iter();
for m in z.flatten() {
println!("{}", m);
}
// Using the `None` variant should not trigger the lint
// Note: for an autofixable suggestion, the binding in the for loop has to take the
// name of the binding in the `if let`
let z = vec![Some(1), Some(2), Some(3)];
for n in z {
if n.is_none() {
println!("Nada.");
}
}
// Using the `Err` variant should not trigger the lint
for n in y.clone() {
if let Err(e) = n {
println!("Oops: {}!", e);
}
}
// Having an else clause should not trigger the lint
for n in y.clone() {
if let Ok(n) = n {
println!("{}", n);
} else {
println!("Oops!");
}
}
let vec_of_ref = vec![&Some(1)];
for n in vec_of_ref.iter().copied().flatten() {
println!("{:?}", n);
}
let vec_of_ref = &vec_of_ref;
for n in vec_of_ref.iter().copied().flatten() {
println!("{:?}", n);
}
let slice_of_ref = &[&Some(1)];
for n in slice_of_ref.iter().copied().flatten() {
println!("{:?}", n);
}
struct Test {
a: usize,
}
let mut vec_of_struct = [Some(Test { a: 1 }), None];
// Usage of `if let` expression should not trigger lint
for n in vec_of_struct.iter_mut() {
if let Some(z) = n {
*n = None;
}
}
// Using manual flatten should not trigger the lint
for n in vec![Some(1), Some(2), Some(3)].iter().flatten() {
println!("{}", n);
}
// Using nested `Some` pattern should not trigger the lint
for n in vec![Some((1, Some(2)))] {
if let Some((_, Some(n))) = n {
println!("{}", n);
}
}
macro_rules! inner {
($id:ident / $new:pat => $action:expr) => {
if let Some($new) = $id {
$action;
}
};
}
// Usage of `if let` expression with macro should not trigger lint
for ab in [Some((1, 2)), Some((3, 4))] {
inner!(ab / (c, d) => println!("{c}-{d}"));
}
macro_rules! args {
($($arg:expr),*) => {
vec![$(Some($arg)),*]
};
}
// Usage of `if let` expression with macro should not trigger lint
for n in args!(1, 2, 3) {
if let Some(n) = n {
println!("{:?}", n);
}
}
// This should trigger the lint, but the applicability is `MaybeIncorrect`
let z = vec![Some(1), Some(2), Some(3)];
for n in z.into_iter().flatten() {
println!("{:?}", n);
}
run_unformatted_tests();
}
#[rustfmt::skip]
fn run_unformatted_tests() {
// Skip rustfmt here on purpose so the suggestion does not fit in one line
for n in vec![
//~^ manual_flatten
Some(1),
Some(2),
Some(3)
].iter().flatten() {
println!("{:?}", n);
}
}
|