Open
Description
I tried this code: manual_flatten
fn main() {
let x: Vec<Option<(i32, Option<i32>)>> = vec![Some((4, Some(0)))];
for n in x.iter() {
if let Some((_, Some(n))) = n {
println!("{}", n);
}
}
}
Clippy warns here:
warning: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> bad.rs:3:5
|
3 | for n in x.iter() {
| ^ -------- help: try: `x.iter().flatten()`
| _____|
| |
4 | | if let Some((_, Some(n))) = n {
5 | | println!("{}", n);
6 | | }
7 | | }
| |_____^
|
= note: `#[warn(clippy::manual_flatten)]` on by default
help: ...and remove the `if let` statement in the for loop
--> bad.rs:4:9
|
4 | / if let Some((_, Some(n))) = n {
5 | | println!("{}", n);
6 | | }
| |_________^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten
but we cannot remove the entire if let
because .flatten()
only peels off the outer Some()
.
The "fixed" code would be
fn main() {
let x: Vec<Option<(i32, Option<i32>)>> = vec![Some((4, Some(0)))];
for n in x.iter().flatten() {
if let (_, Some(n)) = n {
println!("{}", n);
}
}
}
but despite what the lint suggestion said, we still need the if let
!