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

Don't advise cloning when using relational operators #110500

Closed
pommicket opened this issue Apr 18, 2023 · 1 comment · Fixed by #110550
Closed

Don't advise cloning when using relational operators #110500

pommicket opened this issue Apr 18, 2023 · 1 comment · Fixed by #110550
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pommicket
Copy link
Contributor

pommicket commented Apr 18, 2023

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

@pommicket pommicket 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 18, 2023
@rustbot rustbot added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Apr 18, 2023
@asquared31415
Copy link
Contributor

asquared31415 commented Apr 18, 2023

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.

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 D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants