Skip to content

Commit

Permalink
Also suggest dereferencing LHS when both &mut T and T are valid binop…
Browse files Browse the repository at this point in the history
… LHS
  • Loading branch information
compiler-errors committed Sep 4, 2022
1 parent 7714562 commit 98f4b20
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_typeck/src/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.is_err()
{
err.downgrade_to_delayed_bug();
} else {
// Otherwise, it's valid to suggest dereferencing the LHS here.
err.span_suggestion_verbose(
lhs.span.shrink_to_lo(),
"consider dereferencing the left-hand side of this operation",
"*",
Applicability::MaybeIncorrect,
);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/typeck/assign-non-lval-needs-deref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// issue #101376

use std::ops::AddAssign;
struct Foo;

impl AddAssign<()> for Foo {
fn add_assign(&mut self, _: ()) {}
}

impl AddAssign<()> for &mut Foo {
fn add_assign(&mut self, _: ()) {}
}

fn main() {
(&mut Foo) += ();
//~^ ERROR invalid left-hand side of assignment
//~| NOTE cannot assign to this expression
//~| HELP consider dereferencing the left-hand side of this operation
}
16 changes: 16 additions & 0 deletions src/test/ui/typeck/assign-non-lval-needs-deref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0067]: invalid left-hand side of assignment
--> $DIR/assign-non-lval-needs-deref.rs:15:16
|
LL | (&mut Foo) += ();
| ---------- ^^
| |
| cannot assign to this expression
|
help: consider dereferencing the left-hand side of this operation
|
LL | *(&mut Foo) += ();
| +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0067`.

0 comments on commit 98f4b20

Please sign in to comment.