Skip to content

Advertise matches! as an alternative to PartialEq on E0369 #140287

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

Open
lukaslueg opened this issue Apr 25, 2025 · 0 comments
Open

Advertise matches! as an alternative to PartialEq on E0369 #140287

lukaslueg opened this issue Apr 25, 2025 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lukaslueg
Copy link
Contributor

Code

enum Foobar {
    Foo,
    Bar
}


fn main() {
    // This does not compile due to E0369
    // This only advertised remedy is to impl PartialEq
    assert!(Foobar::Foo != Foobar::Bar);
    
    // ... but this is also possible, and does not require PartialEq
    assert!(!matches!(Foobar::Foo, Foobar::Bar));
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0369]: binary operation `!=` cannot be applied to type `Foobar`
  --> src/main.rs:10:25
   |
10 |     assert!(Foobar::Foo != Foobar::Bar);
   |             ----------- ^^ ----------- Foobar
   |             |
   |             Foobar
   |
note: an implementation of `PartialEq` might be missing for `Foobar`
  --> src/main.rs:1:1
   |
1  | enum Foobar {
   | ^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `Foobar` with `#[derive(PartialEq)]`
   |
1  + #[derive(PartialEq)]
2  | enum Foobar {
   |

For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

The binary operation can be avoided altogether by using structural equality (e.g. `matches!(Foobar::Foo, Foobar::Bar)`). It'd be nice if E0369 could detect that there are two remedies to the problem:

* Implement `PartialEq` as currently advertised, and keep the binary operation
* Use `matches!` instead of `PartialEq`.

Rationale and extra context

People coming from other programming languages might not be aware that by-value comparison (requiring PartialEq) and pattern-matching aren't the same thing. Clippy warns-by-default specifically on Option<T> == None (looking at you, Python!).

IMHO the help on E0369 should advertise both solutions here; it's an anti-pattern to teach people that by-value comparison is required when they are actually doing structural comparison.

Other cases

Rust Version

Rust 1.86 stable

Anything else?

playground

@lukaslueg lukaslueg added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant