Skip to content

Detect when mut arg: &Type should have been arg: &mut Type #112357

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

Closed
estebank opened this issue Jun 6, 2023 · 0 comments · Fixed by #134977
Closed

Detect when mut arg: &Type should have been arg: &mut Type #112357

estebank opened this issue Jun 6, 2023 · 0 comments · Fixed by #134977
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jun 6, 2023

Code

struct Object;

fn change_object(mut object: &Object) {
    let object2 = Object;
    object = object2;
    // and as follow up object = &object2;
}

fn main() {
    let mut object = Object;
    change_object(&object);
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:5:14
  |
3 | fn change_object(mut object: &Object) {
  |                              ------- expected due to this parameter type
4 |     let object2 = Object;
5 |     object = object2;
  |              ^^^^^^^
  |              |
  |              expected `&Object`, found `Object`
  |              help: consider borrowing here: `&object2`


------


error[E0597]: `object2` does not live long enough
 --> src/main.rs:5:14
  |
3 | fn change_object(mut object: &Object) {
  |                              - let's call the lifetime of this reference `'1`
4 |     let object2 = Object;
  |         ------- binding `object2` declared here
5 |     object = &object2;
  |     ---------^^^^^^^^
  |     |        |
  |     |        borrowed value does not live long enough
  |     assignment requires that `object2` is borrowed for `'1`
6 | }
  | - `object2` dropped here while still borrowed

Desired output

error[E0308]: mismatched types
 --> src/main.rs:5:14
  |
3 | fn change_object(mut object: &Object) {
  |                              ------- expected due to this parameter type
4 |     let object2 = Object;
5 |     object = object2;
  |              ^^^^^^^ expected `&Object`, found `Object`
help: you might have meant to change the `Object` that `object` points to
  |
3 ~ fn change_object(object: &mut Object) {
4 |     let object2 = Object;
5 ~     *object = object2;
  |



----


error[E0597]: `object2` does not live long enough
 --> src/main.rs:5:14
  |
3 | fn change_object(mut object: &Object) {
  |                              - let's call the lifetime of this reference `'1`
4 |     let object2 = Object;
  |         ------- binding `object2` declared here
5 |     object = &object2;
  |     ---------^^^^^^^^
  |     |        |
  |     |        borrowed value does not live long enough
  |     assignment requires that `object2` is borrowed for `'1`
6 | }
  | - `object2` dropped here while still borrowed
help: you might have meant to change the `Object` that `object` points to
  |
3 ~ fn change_object(object: &mut Object) {
4 |     let object2 = Object;
5 ~     *object = object2;
  |

Rationale and extra context

I've seen this case done by a couple of newcomers. For the first case we might want to give both suggestions, depending on how targeted we can actually make this.

Other cases

No response

Anything else?

No response

@estebank estebank 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. D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jun 6, 2023
@bors bors closed this as completed in 54c324f Jan 14, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 14, 2025
Rollup merge of rust-lang#134977 - estebank:issue-112357, r=BoxyUwU

Detect `mut arg: &Ty` meant to be `arg: &mut Ty` and provide structured suggestion

When a newcomer attempts to use an "out parameter" using borrows, they sometimes get confused and instead of mutating the borrow they try to mutate the function-local binding instead. This leads to either type errors (due to assigning an owned value to a mutable binding of reference type) or a multitude of lifetime errors and unused binding warnings.

This change adds a suggestion to the type error

```
error[E0308]: mismatched types
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:6:14
   |
LL | fn change_object(mut object: &Object) {
   |                              ------- expected due to this parameter type
LL |     let object2 = Object;
LL |     object = object2;
   |              ^^^^^^^ expected `&Object`, found `Object`
   |
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_object(object: &mut Object) {
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```
and to the unused assignment lint
```
error: value assigned to `object` is never read
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:11:5
   |
LL |     object = &object2;
   |     ^^^^^^
   |
note: the lint level is defined here
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:1:9
   |
LL | #![deny(unused_assignments, unused_variables)]
   |         ^^^^^^^^^^^^^^^^^^
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_object2(object: &mut Object) {
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```

Fix rust-lang#112357.
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 D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

1 participant