Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cyrgani opened this issue Jan 24, 2025 · 1 comment · Fixed by #136032
Closed

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

cyrgani opened this issue Jan 24, 2025 · 1 comment · Fixed by #136032
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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.

Comments

@cyrgani
Copy link
Contributor

cyrgani commented Jan 24, 2025

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

@cyrgani cyrgani added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 24, 2025
@cyrgani
Copy link
Contributor Author

cyrgani commented Jan 24, 2025

@rustbot label:D-incorrect

@rustbot rustbot added the D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. label Jan 24, 2025
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jan 24, 2025
@estebank estebank self-assigned this Jan 24, 2025
@ValonPrime ValonPrime marked this as a duplicate of #136026 Jan 25, 2025
jhpratt added a commit to jhpratt/rust that referenced this issue Jan 26, 2025
Account for mutable borrow in argument suggestion

```
error: value assigned to `object` is never read
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5
   |
LL |     object = &mut object2;
   |     ^^^^^^
   |
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
   |
LL ~ fn change_object3(object: &mut Object) {
LL |
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```
instead of
```
error: value assigned to `object` is never read
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5
   |
LL |     object = &mut object2;
   |     ^^^^^^
   |
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
   |
LL ~ fn change_object3(object: &mut mut Object) {
LL |
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```

Fix rust-lang#136028.
@bors bors closed this as completed in 1dfc437 Jan 26, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 26, 2025
Rollup merge of rust-lang#136032 - estebank:issue-136028, r=SparrowLii

Account for mutable borrow in argument suggestion

```
error: value assigned to `object` is never read
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5
   |
LL |     object = &mut object2;
   |     ^^^^^^
   |
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
   |
LL ~ fn change_object3(object: &mut Object) {
LL |
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```
instead of
```
error: value assigned to `object` is never read
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5
   |
LL |     object = &mut object2;
   |     ^^^^^^
   |
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
   |
LL ~ fn change_object3(object: &mut mut Object) {
LL |
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```

Fix rust-lang#136028.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants