Skip to content

"Add a reference" assist adds an extra ampersand when fixing to a mutable ref #13047

@yedayak

Description

@yedayak

rust-analyzer version: (eg. output of "rust-analyzer: Show RA Version" command, accessible in VSCode via Ctrl/⌘+Shift+P)
0.3.1162-standalone (634cfe3 2022-08-07)

rustc version: (eg. output of rustc -V)
rustc 1.63.0 (4b91a6ea7 2022-08-08)
relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME)
None

When passing a reference to a function that is mutable, RA gives an assist Add reference here, which outputs incorrect code:

fn main() {
    let mut a = 0;
    foo(<|>&a);
}
fn foo(a: &mut i32) -> () {}

->

fn main() {
    let mut a = 0;
    foo(&mut &a);
}

fn foo(a: &mut i32) -> () {}

I looked at the relevant code, and it seems this can be fixed by checking if d.actual is a reference before suggesting the fix. We might also want to add an assist for fixing just the missing mut problem?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions