rust-lang / rust-lang/rust

Advertise `matches!` as an alternative to PartialEq on E0369

Open
#140,287 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue targets the Rust compiler's E0369 diagnostic; start by locating where that error's help text is defined and how its suggestions are tested. Review the existing PartialEq advice and determine how a matches! alternative should be represented, then update the relevant diagnostic tests so the output covers both remedies.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.