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

Overzealous clippy::needless_match with multiple chained if let blocks #13574

Open
scd31 opened this issue Oct 21, 2024 · 3 comments · May be fixed by #13646
Open

Overzealous clippy::needless_match with multiple chained if let blocks #13574

scd31 opened this issue Oct 21, 2024 · 3 comments · May be fixed by #13646
Assignees
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

@scd31
Copy link

scd31 commented Oct 21, 2024

Summary

It seems that needless_match can fire incorrectly with multiple if let blocks chained together.

Lint Name

clippy::needless_match

Reproducer

I tried this code:

pub fn color_text_to_rgb_tuple(text: &str) -> Option<RGBTuple> {
    if let Some(rgb) = full_hex_color_to_rgb_tuple(text) {
        Some(rgb)
    } else if let Some(rgb) = short_hex_color_to_rgb_tuple(text) {
        Some(rgb)
    } else if let Some(rgb) = color_name_to_rgb_tuple(text) {
        Some(rgb)
    } else {
        None
    }
}

I saw this happen:

warning: this if-let expression is unnecessary
  --> src/colors.rs:4:5
   |
4  | /     if let Some(rgb) = full_hex_color_to_rgb_tuple(text) {
5  | |         Some(rgb)
6  | |     } else if let Some(rgb) = short_hex_color_to_rgb_tuple(text) {
7  | |         Some(rgb)
...  |
11 | |         None
12 | |     }
   | |_____^ help: replace it with: `full_hex_color_to_rgb_tuple(text)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_match
   = note: `#[warn(clippy::needless_match)]` on by default

I expected to see this happen: No error

Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Additional Labels

No response

@scd31 scd31 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 Oct 21, 2024
@blyxyas
Copy link
Member

blyxyas commented Oct 21, 2024

Here's a repro:

pub fn A() -> Option<bool> {
    if let Some(a) = None {
        Some(a)
    } else if let Some(a) = None {
        Some(a)
    } else if let Some(a) = None {
        Some(a)
    } else {
        None
    }
}

@SpriteOvO
Copy link
Contributor

@rustbot claim

@SpriteOvO
Copy link
Contributor

@blyxyas It's okay, you go ahead. I didn't start the work, just claimed it for a look at it tomorrow.

@rustbot release-assignment

@blyxyas blyxyas self-assigned this Oct 21, 2024
@blyxyas blyxyas linked a pull request Nov 2, 2024 that will close this issue
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.

3 participants