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

E0308 suggestion can lead to unnecessary clones #114050

Open
aticu opened this issue Jul 25, 2023 · 1 comment · Fixed by #114052
Open

E0308 suggestion can lead to unnecessary clones #114050

aticu opened this issue Jul 25, 2023 · 1 comment · Fixed by #114052
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

@aticu
Copy link
Contributor

aticu commented Jul 25, 2023

Code

fn main() {
    let string = String::from("foo");
    let a = Some(string.clone());
    let b = Some(&string);
    if a == b {
        println!("do stuff");
    }
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:5:13
  |
5 |     if a == b {
  |             ^ expected `Option<String>`, found `Option<&String>`
  |
  = note: expected enum `Option<String>`
             found enum `Option<&String>`
help: use `Option::cloned` to clone the value inside the `Option`
  |
5 |     if a == b.cloned() {
  |              +++++++++

Desired output

error[E0308]: mismatched types
 --> src/main.rs:5:13
  |
5 |     if a == b {
  |             ^ expected `Option<String>`, found `Option<&String>`
  |
  = note: expected enum `Option<String>`
             found enum `Option<&String>`
help: try using `.as_ref()` to convert `Option<String>` to `Option<&String>`
  |
5 |     if a.as_ref() == b {
  |         +++++++++

Rationale and extra context

This works as expected when a and b are swapped, but with this ordering, cloned is suggested, which leads to an allocation/copy which will be immediately freed and is entirely avoidable.

Other cases

a and b swapped leads to the correct output.

Anything else?

Tested on nightly 2023-07-24 31395ec38250b60b380f

@aticu aticu 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 Jul 25, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 25, 2023
@clubby789 clubby789 removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 25, 2023
@bors bors closed this as completed in b7a1ff2 Jul 25, 2023
@compiler-errors
Copy link
Member

Reopening since the fix was reverted

cuviper added a commit to cuviper/rust that referenced this issue Aug 17, 2023
…rors

Revert PR rust-lang#114052 to fix invalid suggestion

This PR reverts rust-lang#114052 to fix the invalid suggestion produced by the PR.

Unfortunately the invalid suggestion cannot be improved from the current position where it's emitted since we lack enough information (is an assignment?, left or right?, ...) to be able to fix it here. Furthermore the previous wasn't wrong, just suboptimal, contrary to the current one which is just wrong.

Added a regression test and commented out some code instead of removing it so we can use it later.

Reopens rust-lang#114050
Fixes rust-lang#114925
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

Successfully merging a pull request may close this issue.

4 participants