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 mut arg: &Ty meant to be arg: &mut Ty and provide structured suggestion #134977

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

estebank
Copy link
Contributor

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 #112357.

@rustbot
Copy link
Collaborator

rustbot commented Dec 31, 2024

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 31, 2024
@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor Author

If this is considered too esoteric, I'm ok with closing the PR and ticket, but it feels directionally correct to me.

This is a mistake I've seen newcomers make where they want to express an "out" argument.
```
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;
   |
```

This might be the first thing someone tries to write to mutate the value *behind* an argument. We avoid suggesting `object = &object2;`, as that is less likely to be what was intended.
@rust-log-analyzer

This comment has been minimized.

```
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;
   |
```

This might be the first thing someone tries to write to mutate the value *behind* an argument, trying to avoid an E0308.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect when mut arg: &Type should have been arg: &mut Type
4 participants