Skip to content

manual_flatten interaction with nested matches could better #6776

@matthiaskrgr

Description

@matthiaskrgr

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!

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions