Skip to content

manual_flatten ignores nested refutable pattern #12645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
crumblingstatue opened this issue Apr 8, 2024 · 1 comment · Fixed by #14846
Closed

manual_flatten ignores nested refutable pattern #12645

crumblingstatue opened this issue Apr 8, 2024 · 1 comment · Fixed by #14846
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@crumblingstatue
Copy link

Summary

The manual_flatten lint doesn't take into account nested refutable patterns.

Lint Name

manual_flatten

Reproducer

I tried this code:

enum Data {
    Wanted(i32),
    Unwanted(String),
}

fn iter() -> impl IntoIterator<Item = Result<Data, ()>> {
    vec![Ok(Data::Wanted(42))]
}

fn main() {
    for item in iter() {
        if let Ok(Data::Wanted(n)) = item {
            println!("{n}");
        }
    }
}

Clippy output:

warning: variant `Unwanted` is never constructed
 --> src/main.rs:3:5
  |
1 | enum Data {
  |      ---- variant in this enum
2 |     Wanted(i32),
3 |     Unwanted(String),
  |     ^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: unnecessary `if let` since only the `Ok` variant of the iterator element is used
  --> src/main.rs:11:5
   |
11 |       for item in iter() {
   |       ^           ------ help: try: `iter().into_iter().flatten()`
   |  _____|
   | |
12 | |         if let Ok(Data::Wanted(n)) = item {
13 | |             println!("{n}");
14 | |         }
15 | |     }
   | |_____^
   |
help: ...and remove the `if let` statement in the for loop
  --> src/main.rs:12:9
   |
12 | /         if let Ok(Data::Wanted(n)) = item {
13 | |             println!("{n}");
14 | |         }
   | |_________^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten
   = note: `#[warn(clippy::manual_flatten)]` on by default

It shouldn't suggest to remove the if let statement, because it's still needed to refutably match Data::Wanted.

Version

rustc 1.79.0-nightly (aa1c45908 2024-04-06)
binary: rustc
commit-hash: aa1c45908df252a5b0c14e1bcb38c6c55ae02efe
commit-date: 2024-04-06
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

Additional Labels

No response

@crumblingstatue crumblingstatue added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 8, 2024
@crumblingstatue
Copy link
Author

I guess technically the suggestion can be applied (without removing the if let though), so I'm having second thoughts about whether this really is a false positive, but the suggestion is definitely misleading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant