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

Detect misparsed binop caused by missing semi #117292

Merged
merged 2 commits into from
Nov 6, 2023
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
27 changes: 27 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MachineApplicable,
);
});
self.check_for_missing_semi(lhs, &mut err);

adjust_err(&mut err);

err.emit();
}

/// Check if the expression that could not be assigned to was a typoed expression that
pub fn check_for_missing_semi(
&self,
expr: &'tcx hir::Expr<'tcx>,
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
) -> bool {
if let hir::ExprKind::Binary(binop, lhs, rhs) = expr.kind
&& let hir::BinOpKind::Mul = binop.node
&& self.tcx.sess.source_map().is_multiline(lhs.span.between(rhs.span))
&& rhs.is_syntactic_place_expr()
{
// v missing semicolon here
// foo()
// *bar = baz;
// (#80446).
err.span_suggestion_verbose(
lhs.span.shrink_to_hi(),
"you might have meant to write a semicolon here",
";".to_string(),
Applicability::MachineApplicable,
);
return true;
}
false
}

// Check if an expression `original_expr_id` comes from the condition of a while loop,
/// as opposed from the body of a while loop, which we can naively check by iterating
/// parents until we find a loop...
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_hir_typeck/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(err, output_def_id)
}
};
if self.check_for_missing_semi(expr, &mut err)
&& let hir::Node::Expr(expr) = self.tcx.hir().get_parent(expr.hir_id)
&& let hir::ExprKind::Assign(..) = expr.kind
{
// We defer to the later error produced by `check_lhs_assignable`.
err.delay_as_bug();
}
estebank marked this conversation as resolved.
Show resolved Hide resolved

let suggest_deref_binop =
|err: &mut DiagnosticBuilder<'_, _>, lhs_deref_ty: Ty<'tcx>| {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/binop/false-binop-caused-by-missing-semi.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix
fn foo() {}
fn main() {
let mut y = 42;
let x = &mut y;
foo();
*x = 0; //~ ERROR invalid left-hand side of assignment
let _ = x;
println!("{y}");
}
10 changes: 10 additions & 0 deletions tests/ui/binop/false-binop-caused-by-missing-semi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix
fn foo() {}
fn main() {
let mut y = 42;
let x = &mut y;
foo()
*x = 0; //~ ERROR invalid left-hand side of assignment
let _ = x;
println!("{y}");
}
17 changes: 17 additions & 0 deletions tests/ui/binop/false-binop-caused-by-missing-semi.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0070]: invalid left-hand side of assignment
--> $DIR/false-binop-caused-by-missing-semi.rs:7:8
|
LL | / foo()
LL | | *x = 0;
| | - ^
| |______|
| cannot assign to this expression
|
help: you might have meant to write a semicolon here
|
LL | foo();
| +

error: aborting due to previous error

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