rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Overzealous clippy::needless_match with multiple chained if let blocks

Open scd31 opened this issue 1 year ago • 0 comments

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 avatar Oct 21 '24 00:10 scd31