Skip to content

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

Open
@antonilol

Description

@antonilol

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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