-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Changes from all commits
57f9f8f
c2ae386
ec98df4
4438b32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//@ run-rustfix | ||
#![deny(unused_assignments, unused_variables)] | ||
struct Object; | ||
|
||
fn change_object(object: &mut Object) { //~ HELP you might have meant to mutate | ||
let object2 = Object; | ||
*object = object2; //~ ERROR mismatched types | ||
} | ||
|
||
fn change_object2(object: &mut Object) { //~ ERROR variable `object` is assigned to, but never used | ||
//~^ HELP you might have meant to mutate | ||
let object2 = Object; | ||
*object = object2; | ||
//~^ ERROR `object2` does not live long enough | ||
//~| ERROR value assigned to `object` is never read | ||
} | ||
|
||
fn main() { | ||
let mut object = Object; | ||
change_object(&mut object); | ||
change_object2(&mut object); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//@ run-rustfix | ||
#![deny(unused_assignments, unused_variables)] | ||
struct Object; | ||
|
||
fn change_object(mut object: &Object) { //~ HELP you might have meant to mutate | ||
let object2 = Object; | ||
object = object2; //~ ERROR mismatched types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you want to add a |
||
} | ||
|
||
fn change_object2(mut object: &Object) { //~ ERROR variable `object` is assigned to, but never used | ||
//~^ HELP you might have meant to mutate | ||
let object2 = Object; | ||
object = &object2; | ||
//~^ ERROR `object2` does not live long enough | ||
//~| ERROR value assigned to `object` is never read | ||
} | ||
|
||
fn main() { | ||
let mut object = Object; | ||
change_object(&mut object); | ||
change_object2(&mut object); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:7: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; | ||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The simple case works though so 🤷♀️ still valuable |
||
| | ||
|
||
error: value assigned to `object` is never read | ||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:13: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:2: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 | | ||
LL | let object2 = Object; | ||
LL ~ *object = object2; | ||
| | ||
|
||
error: variable `object` is assigned to, but never used | ||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:10:23 | ||
| | ||
LL | fn change_object2(mut object: &Object) { | ||
| ^^^^^^ | ||
| | ||
= note: consider using `_object` instead | ||
note: the lint level is defined here | ||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:2:29 | ||
| | ||
LL | #![deny(unused_assignments, unused_variables)] | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error[E0597]: `object2` does not live long enough | ||
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:13:14 | ||
| | ||
LL | fn change_object2(mut object: &Object) { | ||
| - let's call the lifetime of this reference `'1` | ||
LL | | ||
LL | let object2 = Object; | ||
| ------- binding `object2` declared here | ||
LL | object = &object2; | ||
| ---------^^^^^^^^ | ||
| | | | ||
| | borrowed value does not live long enough | ||
| assignment requires that `object2` is borrowed for `'1` | ||
... | ||
LL | } | ||
| - `object2` dropped here while still borrowed | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0308, E0597. | ||
For more information about an error, try `rustc --explain E0308`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
mut_ty
? this isnt a&mut
ref its some&ty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mut_ty
because the second positional field ofRef
isMutTy
, as in "mutability and type". Granted, the name is barely a step abovex
, and arguably would be better if it wasx
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah :3