Skip to content

Commit 4b7aaca

Browse files
committed
Silence redundant error on typo resulting on binop
1 parent 231f935 commit 4b7aaca

File tree

5 files changed

+27
-33
lines changed

5 files changed

+27
-33
lines changed

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
961961
}
962962

963963
/// Check if the expression that could not be assigned to was a typoed expression that
964-
fn check_for_missing_semi(&self, expr: &'tcx hir::Expr<'tcx>, err: &mut Diagnostic) {
964+
pub fn check_for_missing_semi(
965+
&self,
966+
expr: &'tcx hir::Expr<'tcx>,
967+
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
968+
) -> bool {
965969
if let hir::ExprKind::Binary(binop, lhs, rhs) = expr.kind
966970
&& let hir::BinOpKind::Mul = binop.node
967971
&& self.tcx.sess.source_map().is_multiline(lhs.span.between(rhs.span))
@@ -977,7 +981,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
977981
";".to_string(),
978982
Applicability::MachineApplicable,
979983
);
984+
return true;
980985
}
986+
false
981987
}
982988

983989
// Check if an expression `original_expr_id` comes from the condition of a while loop,

Diff for: compiler/rustc_hir_typeck/src/op.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
379379
(err, output_def_id)
380380
}
381381
};
382-
if self.tcx.sess.source_map().is_multiline(lhs_expr.span.between(rhs_expr.span))
383-
&& let IsAssign::No = is_assign
384-
&& let hir::BinOpKind::Mul = op.node
385-
&& rhs_expr.is_syntactic_place_expr()
382+
if self.check_for_missing_semi(expr, &mut err)
383+
&& let hir::Node::Expr(expr) = self.tcx.hir().get_parent(expr.hir_id)
384+
&& let hir::ExprKind::Assign(..) = expr.kind
386385
{
387-
// v missing semicolon here
388-
// foo()
389-
// *bar = baz;
390-
// (#80446).
391-
err.span_suggestion_verbose(
392-
lhs_expr.span.shrink_to_hi(),
393-
"you might have meant to write a semicolon here",
394-
";".to_string(),
395-
Applicability::MaybeIncorrect,
396-
);
386+
// We defer to the later error produced by `check_lhs_assignable`.
387+
err.delay_as_bug();
397388
}
398389

399390
let suggest_deref_binop =
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
fn foo() {}
3+
fn main() {
4+
let mut y = 42;
5+
let x = &mut y;
6+
foo();
7+
*x = 0; //~ ERROR invalid left-hand side of assignment
8+
let _ = x;
9+
println!("{y}");
10+
}

Diff for: tests/ui/binop/false-binop-caused-by-missing-semi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
// run-rustfix
12
fn foo() {}
23
fn main() {
34
let mut y = 42;
45
let x = &mut y;
56
foo()
67
*x = 0; //~ ERROR invalid left-hand side of assignment
7-
//~^ ERROR cannot multiply `()` by `&mut {integer}`
8+
let _ = x;
89
println!("{y}");
910
}
+3-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
error[E0369]: cannot multiply `()` by `&mut {integer}`
2-
--> $DIR/false-binop-caused-by-missing-semi.rs:6:5
3-
|
4-
LL | foo()
5-
| ----- ()
6-
LL | *x = 0;
7-
| ^- &mut {integer}
8-
|
9-
help: you might have meant to write a semicolon here
10-
|
11-
LL | foo();
12-
| +
13-
141
error[E0070]: invalid left-hand side of assignment
15-
--> $DIR/false-binop-caused-by-missing-semi.rs:6:8
2+
--> $DIR/false-binop-caused-by-missing-semi.rs:7:8
163
|
174
LL | / foo()
185
LL | | *x = 0;
@@ -25,7 +12,6 @@ help: you might have meant to write a semicolon here
2512
LL | foo();
2613
| +
2714

28-
error: aborting due to 2 previous errors
15+
error: aborting due to previous error
2916

30-
Some errors have detailed explanations: E0070, E0369.
31-
For more information about an error, try `rustc --explain E0070`.
17+
For more information about this error, try `rustc --explain E0070`.

0 commit comments

Comments
 (0)