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 in nightly (2022-03-14) ignores enum arguments #8542

Closed
vakaras opened this issue Mar 15, 2022 · 3 comments · Fixed by #8549
Closed

needless_match in nightly (2022-03-14) ignores enum arguments #8542

vakaras opened this issue Mar 15, 2022 · 3 comments · Fixed by #8549
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

@vakaras
Copy link

vakaras commented Mar 15, 2022

Summary

When a matched enum has multiple arguments and one of them is replaced, the needless_match lint still reports an error even though the match is not needless.

Lint Name

needless_match

Reproducer

I tried this code:

pub enum E {
    Variant1(u32, bool),
    Variant2(u32, bool),
}

impl E {
    pub fn set_b(self, b: bool) -> Self {
        match self {
            E::Variant1(a, _) => E::Variant1(a, b),
            E::Variant2(a, _) => E::Variant2(a, b),
        }
    }
}

I saw this happen:

> cargo clippy
    Checking needless_match v0.1.0 (/tmp/bla/needless_match)
warning: this match expression is unnecessary
  --> src/lib.rs:9:9
   |
9  | /         match self {
10 | |             E::Variant1(a, _) => E::Variant1(a, b),
11 | |             E::Variant2(a, _) => E::Variant2(a, b),
12 | |         }
   | |_________^ help: replace it with: `self`
   |
   = 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

warning: `needless_match` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s

I expected to see this happen: no warning should be emitted because the new value is constructed with a new b.

Version

rustc 1.61.0-nightly (285fa7ecd 2022-03-14)
binary: rustc
commit-hash: 285fa7ecd05dcbfdaf2faaf20400f5f92b39b3c6
commit-date: 2022-03-14
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

No response

@vakaras vakaras 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 Mar 15, 2022
@lqd
Copy link
Member

lqd commented Mar 15, 2022

The lint also now triggers for code like this (playground):

pub fn side_effect() -> Option<String> {
    if let Some(r) = op() {
        Some(r)
    } else {
        println!("side-effect");
        None
    }
}

for an unnecessary if-let expression

warning: this if-let expression is unnecessary
  [--> src/lib.rs:6:5
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1c35b2a92d8c00e259973cd1f5c1ef38#)   |
6  | /     if let Some(r) = op() {
7  | |         Some(r)
8  | |     } else {
9  | |         println!("side-effect");
10 | |         None
11 | |     }
   | |_____^ help: replace it with: `op()`
   |
   = note: `#[warn(clippy::needless_match)]` on by default

@J-ZhengLi
Copy link
Member

@rustbot claim

@teor2345
Copy link
Contributor

The lint also triggers on nightly-2022-03-14 for code like this (playground):

 enum Resultt<T, E> {
     Okk(T),
     Errr(E),
 }
 
 use Resultt::*;
 
 impl<T, E> Resultt<T, E> {
    pub const fn as_ref(self: &Resultt<T, E>) -> Resultt<&T, &E> {
        match self {
            Okk(x) => Okk(x),
            Errr(x) => Errr(x),
        }
    }
}

std::Result does it slightly differently, and doesn't trigger the lint:

    pub const fn as_ref(self: &Result<T, E>) -> Result<&T, &E> {
        match *self {
            Ok(ref x) => Ok(x),
            Err(ref x) => Err(x),
        }
    }

(I've desugared &self, to make it clear what the type change is.)

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.

4 participants