-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Suggest dereferencing non-lval mutable reference on assignment #94639
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,6 @@ LL | let x = |ref x: isize| { x += 1; }; | |
| -^^^^^ | ||
| | | ||
| cannot use `+=` on type `&isize` | ||
| | ||
help: `+=` can be used on `isize`, you can dereference `x` | ||
| | ||
LL | let x = |ref x: isize| { *x += 1; }; | ||
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. This is kinda a regression, but not actually... |
||
| + | ||
|
||
error: aborting due to previous error | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
let x = std::sync::Mutex::new(1usize); | ||
*x.lock().unwrap() = 2; | ||
//~^ ERROR invalid left-hand side of assignment | ||
*x.lock().unwrap() += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` | ||
|
||
let mut y = x.lock().unwrap(); | ||
*y = 2; | ||
//~^ ERROR mismatched types | ||
*y += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
let x = std::sync::Mutex::new(1usize); | ||
x.lock().unwrap() = 2; | ||
//~^ ERROR invalid left-hand side of assignment | ||
x.lock().unwrap() += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` | ||
|
||
let mut y = x.lock().unwrap(); | ||
y = 2; | ||
//~^ ERROR mismatched types | ||
y += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
error[E0070]: invalid left-hand side of assignment | ||
--> $DIR/assign-non-lval-derefmut.rs:5:23 | ||
| | ||
LL | x.lock().unwrap() = 2; | ||
| ----------------- ^ | ||
| | | ||
| cannot assign to this expression | ||
| | ||
help: consider dereferencing here to assign to the mutably borrowed value | ||
| | ||
LL | *x.lock().unwrap() = 2; | ||
| + | ||
|
||
error[E0368]: binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` | ||
--> $DIR/assign-non-lval-derefmut.rs:7:5 | ||
| | ||
LL | x.lock().unwrap() += 1; | ||
| -----------------^^^^^ | ||
| | | ||
| cannot use `+=` on type `MutexGuard<'_, usize>` | ||
| | ||
help: `+=` can be used on `usize`, you can dereference `x.lock().unwrap()` | ||
| | ||
LL | *x.lock().unwrap() += 1; | ||
| + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/assign-non-lval-derefmut.rs:11:9 | ||
| | ||
LL | let mut y = x.lock().unwrap(); | ||
| ----------------- expected due to this value | ||
LL | y = 2; | ||
| ^ expected struct `MutexGuard`, found integer | ||
| | ||
= note: expected struct `MutexGuard<'_, usize>` | ||
found type `{integer}` | ||
help: consider dereferencing here to assign to the mutably borrowed value | ||
| | ||
LL | *y = 2; | ||
| + | ||
|
||
error[E0368]: binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>` | ||
--> $DIR/assign-non-lval-derefmut.rs:13:5 | ||
| | ||
LL | y += 1; | ||
| -^^^^^ | ||
| | | ||
| cannot use `+=` on type `MutexGuard<'_, usize>` | ||
| | ||
help: `+=` can be used on `usize`, you can dereference `y` | ||
| | ||
LL | *y += 1; | ||
| + | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0070, E0308, E0368. | ||
For more information about an error, try `rustc --explain E0070`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
let mut x = vec![1usize]; | ||
*x.last_mut().unwrap() = 2; | ||
//~^ ERROR invalid left-hand side of assignment | ||
*x.last_mut().unwrap() += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` | ||
|
||
let y = x.last_mut().unwrap(); | ||
*y = 2; | ||
//~^ ERROR mismatched types | ||
*y += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
let mut x = vec![1usize]; | ||
x.last_mut().unwrap() = 2; | ||
//~^ ERROR invalid left-hand side of assignment | ||
x.last_mut().unwrap() += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` | ||
|
||
let y = x.last_mut().unwrap(); | ||
y = 2; | ||
//~^ ERROR mismatched types | ||
y += 1; | ||
//~^ ERROR binary assignment operation `+=` cannot be applied to type `&mut usize` | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
@compiler-errors For future reference, adding a
delay_span_bug
here might be a good idea to protect from miscompilations in the face of refactors.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.
shoot, yeah. i can add one in a follow-up.
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.
Actually, this just suppresses a suggestion, not the error itself. And the other case I actually did suppress a real error, I downgraded it to a delayed bug. See: https://github.com/rust-lang/rust/pull/94639/files#diff-c46757032f463a1170b40e5c41d569ce058668cfe259774fb8a7936e3fdd82f4R57
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.
@compiler-errors perfect!