Skip to content

Compiler suggests dereferencing LHS of tuple-assign #109991

Closed
@Patryk27

Description

@Patryk27

Code

fn main() {
    let a: usize;
    let b: usize;
    
    (a, b) = (123, &mut 123);
}

Current output

error[E0308]: mismatched types
  --> src/main.rs:17:9
   |
15 |     let b: usize;
   |            ----- expected due to this type
16 |     
17 |     (a, b) = (123, &mut 123);
   |         ^ expected `usize`, found `&mut {integer}`
   |
help: consider dereferencing the borrow
   |
17 |     (a, *b) = (123, &mut 123);
   |         +

Desired output

error[E0308]: mismatched types
  --> src/main.rs:17:9
   |
15 |     let b: usize;
   |            ----- expected due to this type
16 |     
17 |     (a, b) = (123, &mut 123);
   |         ^ expected `usize`, found `&mut {integer}`
   |
help: consider dereferencing the borrow
   |
17 |     (a, b) = (123, *&mut 123);
   |                    +

Although this cannot be always applied, e.g. consider:

fn main() {
    let a: usize;
    let b: usize;
    
    (a, b) = foo();
}

fn foo() -> (usize, &'static mut usize) {
    todo!()
}

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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

No branches or pull requests

Issue actions