about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-05-23 19:22:55 -0400
committerMichael Goulet <michael@errs.io>2024-05-23 19:22:55 -0400
commita02aba7c542bf65f53c3a631f56110d8d95afc1c (patch)
tree4daaadbfc128abf70143bacd19a1c92a690c54ba
parent5baee04b6349d176440cb1fcd5424a89f67b9f7b (diff)
downloadrust-a02aba7c542bf65f53c3a631f56110d8d95afc1c.tar.gz
rust-a02aba7c542bf65f53c3a631f56110d8d95afc1c.zip
Only suppress binop error in favor of semicolon suggestion if we're in an assignment statement
-rw-r--r--compiler/rustc_hir_typeck/src/op.rs5
-rw-r--r--tests/ui/binop/nested-assignment-may-be-deref.rs14
-rw-r--r--tests/ui/binop/nested-assignment-may-be-deref.stderr29
3 files changed, 47 insertions, 1 deletions
diff --git a/compiler/rustc_hir_typeck/src/op.rs b/compiler/rustc_hir_typeck/src/op.rs
index 87b76b978b9..25b74dca12f 100644
--- a/compiler/rustc_hir_typeck/src/op.rs
+++ b/compiler/rustc_hir_typeck/src/op.rs
@@ -381,10 +381,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 let maybe_missing_semi = self.check_for_missing_semi(expr, &mut err);
 
                 // We defer to the later error produced by `check_lhs_assignable`.
-                // We only downgrade this if it's the LHS, though.
+                // We only downgrade this if it's the LHS, though, and if this is a
+                // valid assignment statement.
                 if maybe_missing_semi
                     && let hir::Node::Expr(parent) = self.tcx.parent_hir_node(expr.hir_id)
                     && let hir::ExprKind::Assign(lhs, _, _) = parent.kind
+                    && let hir::Node::Stmt(stmt) = self.tcx.parent_hir_node(parent.hir_id)
+                    && let hir::StmtKind::Expr(_) | hir::StmtKind::Semi(_) = stmt.kind
                     && lhs.hir_id == expr.hir_id
                 {
                     err.downgrade_to_delayed_bug();
diff --git a/tests/ui/binop/nested-assignment-may-be-deref.rs b/tests/ui/binop/nested-assignment-may-be-deref.rs
new file mode 100644
index 00000000000..f675ab2e918
--- /dev/null
+++ b/tests/ui/binop/nested-assignment-may-be-deref.rs
@@ -0,0 +1,14 @@
+pub fn bad(x: &mut bool) {
+    if true
+    *x = true {}
+    //~^ ERROR cannot multiply `bool` by `&mut bool`
+}
+
+pub fn bad2(x: &mut bool) {
+    let y: bool;
+    y = true
+    *x = true;
+    //~^ ERROR cannot multiply `bool` by `&mut bool`
+}
+
+fn main() {}
diff --git a/tests/ui/binop/nested-assignment-may-be-deref.stderr b/tests/ui/binop/nested-assignment-may-be-deref.stderr
new file mode 100644
index 00000000000..95b2db2b26c
--- /dev/null
+++ b/tests/ui/binop/nested-assignment-may-be-deref.stderr
@@ -0,0 +1,29 @@
+error[E0369]: cannot multiply `bool` by `&mut bool`
+  --> $DIR/nested-assignment-may-be-deref.rs:3:5
+   |
+LL |     if true
+   |        ---- bool
+LL |     *x = true {}
+   |     ^- &mut bool
+   |
+help: you might have meant to write a semicolon here
+   |
+LL |     if true;
+   |            +
+
+error[E0369]: cannot multiply `bool` by `&mut bool`
+  --> $DIR/nested-assignment-may-be-deref.rs:10:5
+   |
+LL |     y = true
+   |         ---- bool
+LL |     *x = true;
+   |     ^- &mut bool
+   |
+help: you might have meant to write a semicolon here
+   |
+LL |     y = true;
+   |             +
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0369`.