Skip to content

Don't advise cloning when using relational operators #110500

Closed
@pommicket

Description

@pommicket

Code

#[derive(PartialEq, Eq, Clone)]
struct Foo {}

fn main() {
    let foo = Foo {};
    let bar = &Foo {};

    if foo == bar {
        println!("yay");
    }
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:9:12
  |
9 |     if foo == bar {
  |        ---    ^^^ expected struct `Foo`, found `&Foo`
  |        |
  |        expected because this is `Foo`
  |
help: consider using clone here
  |
9 |     if foo == bar.clone() {
  |                  ++++++++

Desired output

error[E0308]: mismatched types
 --> src/main.rs:9:12
  |
9 |     if foo == bar {
  |        ---    ^^^ expected struct `Foo`, found `&Foo`
  |        |
  |        expected because this is `Foo`
  |
help: consider dereferencing bar
  |
9 |     if foo == *bar {
  |               +

Rationale and extra context

when using relational operators a clone is not necessary, and just dereferencing is more performant (for large types, obviously not in this case) and more readable.

Other cases

No response

Anything else?

No response

@rustbot modify labels +D-papercut

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions