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

hint to consider dereferencing instead of mutably borrowing an immutable variable #112958

Open
antonilol opened this issue Jun 23, 2023 · 0 comments
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

@antonilol
Copy link
Contributor

Code

let a = &mut 0;
let b = 1;
let r = &b;
let z = a < b;

println!("{r}");

Current output

error[E0308]: mismatched types
 --> src/main.rs:5:13
  |
5 | let z = a < b;
  |             ^ expected `&mut _`, found integer
  |
  = note: expected mutable reference `&mut _`
                          found type `{integer}`
help: consider mutably borrowing here
  |
5 | let z = a < &mut b;
  |             ++++

For more information about this error, try `rustc --explain E0308`.

Desired output

something along the lines of:

let z = a < b;
        ^ expected `integer`, found &mut _

help: consider dereferencing here
  |
5 | let z = *a < b;
  |         +

For more information about this error, try `rustc --explain E0308`.

Rationale and extra context

rustc should not hint to mutably borrow an immutable variable b (or also tell to make it mutable, but this does not work if b is borrowed), it should hint to dereference a (*a < b) or convert a to an immutable reference and reference b (&*a < &b)

Other cases

No response

Anything else?

the same message with stable, beta and nightly, only stable doesnt show where to insert &mut

@antonilol antonilol 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 Jun 23, 2023
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.

1 participant