Skip to content

Commit a02aba7

Browse files
Only suppress binop error in favor of semicolon suggestion if we're in an assignment statement
1 parent 5baee04 commit a02aba7

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

compiler/rustc_hir_typeck/src/op.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
381381
let maybe_missing_semi = self.check_for_missing_semi(expr, &mut err);
382382

383383
// We defer to the later error produced by `check_lhs_assignable`.
384-
// We only downgrade this if it's the LHS, though.
384+
// We only downgrade this if it's the LHS, though, and if this is a
385+
// valid assignment statement.
385386
if maybe_missing_semi
386387
&& let hir::Node::Expr(parent) = self.tcx.parent_hir_node(expr.hir_id)
387388
&& let hir::ExprKind::Assign(lhs, _, _) = parent.kind
389+
&& let hir::Node::Stmt(stmt) = self.tcx.parent_hir_node(parent.hir_id)
390+
&& let hir::StmtKind::Expr(_) | hir::StmtKind::Semi(_) = stmt.kind
388391
&& lhs.hir_id == expr.hir_id
389392
{
390393
err.downgrade_to_delayed_bug();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn bad(x: &mut bool) {
2+
if true
3+
*x = true {}
4+
//~^ ERROR cannot multiply `bool` by `&mut bool`
5+
}
6+
7+
pub fn bad2(x: &mut bool) {
8+
let y: bool;
9+
y = true
10+
*x = true;
11+
//~^ ERROR cannot multiply `bool` by `&mut bool`
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0369]: cannot multiply `bool` by `&mut bool`
2+
--> $DIR/nested-assignment-may-be-deref.rs:3:5
3+
|
4+
LL | if true
5+
| ---- bool
6+
LL | *x = true {}
7+
| ^- &mut bool
8+
|
9+
help: you might have meant to write a semicolon here
10+
|
11+
LL | if true;
12+
| +
13+
14+
error[E0369]: cannot multiply `bool` by `&mut bool`
15+
--> $DIR/nested-assignment-may-be-deref.rs:10:5
16+
|
17+
LL | y = true
18+
| ---- bool
19+
LL | *x = true;
20+
| ^- &mut bool
21+
|
22+
help: you might have meant to write a semicolon here
23+
|
24+
LL | y = true;
25+
| +
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0369`.

0 commit comments

Comments
 (0)