Skip to content
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

needless_match doesn't consider the else of if let #8695

Closed
goffrie opened this issue Apr 13, 2022 · 3 comments · Fixed by #8700
Closed

needless_match doesn't consider the else of if let #8695

goffrie opened this issue Apr 13, 2022 · 3 comments · Fixed by #8700
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

@goffrie
Copy link

goffrie commented Apr 13, 2022

Summary

The needless_match lint produces a nonsensical suggestion in some cases. It seems like it fires entirely based on the if-clause of an if let, ignoring the else branch's value entirely. This is a regression, most likely from #8549 (but I haven't confirmed that).

Lint Name

needless_match

Reproducer

I tried this code:

pub fn bad() -> Option<i32> {
    if let Some(existing) = None {
        Some(existing)
    } else {
        Some(123)
    }
}

I saw this happen:

warning: this if-let expression is unnecessary
 [--> src/lib.rs:2:5
](https://play.rust-lang.org/#)  |
2 | /     if let Some(existing) = None {
3 | |         Some(existing)
4 | |     } else {
5 | |         Some(123)
6 | |     }
  | |_____^ help: replace it with: `None`
  |
  = note: `#[warn(clippy::needless_match)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_match

I expected to see no lint error.

Version

rustc 1.62.0-nightly (1f7fb6413 2022-04-10)
binary: rustc
commit-hash: 1f7fb6413d6d6c0c929b223e478e44c3db991b03
commit-date: 2022-04-10
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.0

Additional Labels

No response

@goffrie goffrie 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 13, 2022
@J-ZhengLi
Copy link
Member

@rustbot claim

@fanninpm
Copy link

Does #8700 fix this?

@goffrie
Copy link
Author

goffrie commented Apr 15, 2022

Does #8700 fix this?

Yes, I think it does.

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
3 participants