Skip to content
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

Only suppress binop error in favor of semicolon suggestion if we're in an assignment statement #125467

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_hir_typeck/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/binop/nested-assignment-may-be-deref.rs
Original file line number Diff line number Diff line change
@@ -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() {}
29 changes: 29 additions & 0 deletions tests/ui/binop/nested-assignment-may-be-deref.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading