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

single_match shouldn't warn when matching on an explicit error type #13371

Open
stevenengler opened this issue Sep 8, 2024 · 0 comments
Open
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

@stevenengler
Copy link
Contributor

stevenengler commented Sep 8, 2024

Summary

In the following code, I think the clippy suggestion is worse than the original code. The original code is explicitly matching on the error "NotFoundError". If the error return type of foo were to change in the future (for example to become an enum with multiple error variants, or to become something like "NotAuthenticatedError" instead), we'd want this match statement to fail to build so that we could update it for the new error type and make sure we're handling it correctly. The clippy suggestion wants us to remove the error matching completely.

I think it would be nicer if this lint only triggered when the error type is omitted completely, for example Err(_) => {}.

Lint Name

single_match

Reproducer

I tried this code:

struct NotFoundError;

fn bar() -> Result<(), NotFoundError> { todo!() }

fn main() {
    match bar() {
        Ok(()) => {},
        // don't need to do anything if not found
        Err(NotFoundError) => {},
    }
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=421e3fe89d0da5e2685c435d43246689

I saw this happen:

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
  --> src/main.rs:6:5
   |
6  | /     match bar() {
7  | |         Ok(()) => {},
8  | |         // don't need to do anything if not found
9  | |         Err(NotFoundError) => {},
10 | |     }
   | |_____^ help: try: `if let Ok(()) = bar() {}`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match
   = note: `#[warn(clippy::single_match)]` on by default

I expected to see this happen:

No warning.

Version

rustc 1.83.0-nightly (26b5599e4 2024-09-06)
binary: rustc
commit-hash: 26b5599e4d6ed2b45152c60493c1788c0a27533d
commit-date: 2024-09-06
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Additional Labels

No response

@stevenengler stevenengler 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 Sep 8, 2024
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

No branches or pull requests

1 participant