Skip to content

New suggestion for overwriting a reference is wrong for mutable references #136028

Closed
@cyrgani

Description

@cyrgani

Code

fn change_val(mut val: &mut i32) {
    val = &mut 10;
}

Current output

warning: value assigned to `val` is never read
 --> src/lib.rs:2:5
  |
2 |     val = &mut 10;
  |     ^^^
  |
  = note: `#[warn(unused_assignments)]` on by default
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
  |
1 ~ fn change_val(val: &mut mut i32) {
2 ~     *val = 10;
  |

<other warning and error elided as they do not matter here>

Desired output

warning: value assigned to `val` is never read
 --> src/lib.rs:2:5
  |
2 |     val = &mut 10;
  |     ^^^
  |
  = note: `#[warn(unused_assignments)]` on by default
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
  |
1 ~ fn change_val(val: &mut i32) {
2 ~     *val = 10;
  |

<other warning and error elided as they do not matter here>

Rationale and extra context

This hint was added in #134977. Encountered this while looking at #136026.
cc @estebank as you added this suggestion

Other cases

Rust Version

1.86.0-nightly (2025-01-23 99768c80a1c094a5cfc3)

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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