Skip to content

Don't advise cloning when using relational operators #110500

@pommicket

Description

@pommicket
Contributor

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

Activity

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

asquared31415 commented on Apr 18, 2023

@asquared31415
Contributor

An alternate suggestion could be &foo == bar. Either way, suggesting Clone is not very helpful here.

Update: I have been informed that there is not a blanket impl that would make this work.

added a commit that references this issue on Apr 26, 2023
8e6fffc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @compiler-errors@pommicket@asquared31415@rustbot

    Issue actions

      Don't advise cloning when using relational operators · Issue #110500 · rust-lang/rust