- Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestionsLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy
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
!
jplatte and lxl66566
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestionsLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy