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 should lint exhaustive match on standard types #8928

Closed
yeputons opened this issue Jun 1, 2022 · 5 comments · Fixed by #8985
Closed

single_match should lint exhaustive match on standard types #8928

yeputons opened this issue Jun 1, 2022 · 5 comments · Fixed by #8985
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@yeputons
Copy link

yeputons commented Jun 1, 2022

Summary

After #8282 was closed via #8322 (in Rust 1.60) the example below is no longer linting.

The original rationale was that Clippy should not lint matches on user-defined enums which explicitly list all possible options. Otherwise extending the enum later would break the original code, but not the fixed one, which may be unexpected for the author.

However, there is little reason to believe that standard types like std::option::Option will be extended in the future. I suggest that they're exempt from this check. Another example is std::result::Result. Of course, that does not help with user-defined Result-like types like in the anyhow crate, but still.

Lint Name

single_match

Reproducer

I tried this code:

fn main() {
    let x = Some(10);
    match x {
        Some(_) => println!("hi!"),
        None => {},
    };
}

I expected to see this happen:

Like in Rust 1.58:

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
 --> a.rs:3:5
  |
3 | /     match x {
4 | |         Some(x) => println!("hi! {}", x),
5 | |         None => {},
6 | |     };
  | |_____^ help: try this: `if let Some(x) = x { println!("hi! {}", x) }`
  |
  = note: `#[warn(clippy::single_match)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match

warning: 1 warning emitted

Instead, this happened:

No output

Version

Clippy 0.1.63 (2022-05-31 e094492)
@yeputons yeputons added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Jun 1, 2022
yeputons added a commit to neondatabase/neon that referenced this issue Jun 1, 2022
* Fix failing `cargo clippy` while we're here.
  The behavior has been changed in Rust 1.60: rust-lang/rust-clippy#8928
* Add Rust version to the Cargo deps cache key
yeputons added a commit to neondatabase/neon that referenced this issue Jun 2, 2022
* Fix failing `cargo clippy` while we're here.
  The behavior has been changed in Rust 1.60: rust-lang/rust-clippy#8928
* Add Rust version to the Cargo deps cache key
yeputons added a commit to neondatabase/neon that referenced this issue Jun 2, 2022
* Fix failing `cargo clippy` while we're here.
  The behavior has been changed in Rust 1.60: rust-lang/rust-clippy#8928
* Add Rust version to the Cargo deps cache key
@botahamec
Copy link
Contributor

@rustbot claim

@botahamec

This comment was marked as resolved.

@botahamec

This comment was marked as resolved.

@yeputons
Copy link
Author

Actually, that fix would result in a compiler error, so yeah, that's definitely not what we want. I'll try fixing it

Seems weird to me. These two lines of code look exactly the same, would you mind showing the compiler error? Probably some lifetime issues, if I were to guess.

@botahamec
Copy link
Contributor

botahamec commented Jun 12, 2022

@yeptuons The problem is that the Err arm creates a variable called errs, which is being used in the block

I don't have the clippy source code on my laptop, but in the playground it looks something like this:

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `errs` in this scope
 --> src/main.rs:3:14
    |
641 |         drop(errs);
    |              ^^^^ not found in this scope

@bors bors closed this as completed in 8789f4e Jun 25, 2022
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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants