Skip to content

Commit 98f4b20

Browse files
Also suggest dereferencing LHS when both &mut T and T are valid binop LHS
1 parent 7714562 commit 98f4b20

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

compiler/rustc_typeck/src/check/op.rs

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7070
.is_err()
7171
{
7272
err.downgrade_to_delayed_bug();
73+
} else {
74+
// Otherwise, it's valid to suggest dereferencing the LHS here.
75+
err.span_suggestion_verbose(
76+
lhs.span.shrink_to_lo(),
77+
"consider dereferencing the left-hand side of this operation",
78+
"*",
79+
Applicability::MaybeIncorrect,
80+
);
7381
}
7482
}
7583
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// issue #101376
2+
3+
use std::ops::AddAssign;
4+
struct Foo;
5+
6+
impl AddAssign<()> for Foo {
7+
fn add_assign(&mut self, _: ()) {}
8+
}
9+
10+
impl AddAssign<()> for &mut Foo {
11+
fn add_assign(&mut self, _: ()) {}
12+
}
13+
14+
fn main() {
15+
(&mut Foo) += ();
16+
//~^ ERROR invalid left-hand side of assignment
17+
//~| NOTE cannot assign to this expression
18+
//~| HELP consider dereferencing the left-hand side of this operation
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0067]: invalid left-hand side of assignment
2+
--> $DIR/assign-non-lval-needs-deref.rs:15:16
3+
|
4+
LL | (&mut Foo) += ();
5+
| ---------- ^^
6+
| |
7+
| cannot assign to this expression
8+
|
9+
help: consider dereferencing the left-hand side of this operation
10+
|
11+
LL | *(&mut Foo) += ();
12+
| +
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0067`.

0 commit comments

Comments
 (0)