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

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

Open
estebank opened this issue Jun 6, 2023 · 0 comments · May be fixed by #134977
Open

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

estebank opened this issue Jun 6, 2023 · 0 comments · May be 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
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