Compiler incorrectly suggests &mut x
instead of &(mut x)
in a pattern for Copy
types.
#122415
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
D-invalid-suggestion
Diagnostics: A structured suggestion resulting in incorrect code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Current output
Desired output
Instead of suggesting
&mut x: &i32
, which doesn't compile, the compiler should suggest&(mut x): &i32
, which does compile.Rationale and extra context
This incorrect suggestion occurs when attempting to use a
&x
pattern to copy aCopy
type (i32
in the reproduction code) from a reference, and then mutate the copy. The correct thing to do is to dereference with a&
pattern, and assign it to amut
binding, which is written as&(mut x)
. The compiler incorrectly suggests&mut x
, which means something else, and doesn't work.The original use case that led to this issue had a one-line closure instead of the
foo
function, which makes doing something likelet mut x = x
undesirable.Other cases
No response
Rust Version
Anything else?
No response
The text was updated successfully, but these errors were encountered: