Silence redundant error on typo resulting on binop

This commit is contained in:
Esteban Küber 2023-10-27 17:27:21 +00:00
parent 231f93524d
commit 4b7aacaa4f
5 changed files with 27 additions and 33 deletions

View File

@ -961,7 +961,11 @@ pub(crate) fn check_lhs_assignable(
}
/// Check if the expression that could not be assigned to was a typoed expression that
fn check_for_missing_semi(&self, expr: &'tcx hir::Expr<'tcx>, err: &mut Diagnostic) {
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))
@ -977,7 +981,9 @@ fn check_for_missing_semi(&self, expr: &'tcx hir::Expr<'tcx>, err: &mut Diagnost
";".to_string(),
Applicability::MachineApplicable,
);
return true;
}
false
}
// Check if an expression `original_expr_id` comes from the condition of a while loop,

View File

@ -379,21 +379,12 @@ fn check_overloaded_binop(
(err, output_def_id)
}
};
if self.tcx.sess.source_map().is_multiline(lhs_expr.span.between(rhs_expr.span))
&& let IsAssign::No = is_assign
&& let hir::BinOpKind::Mul = op.node
&& rhs_expr.is_syntactic_place_expr()
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
{
// v missing semicolon here
// foo()
// *bar = baz;
// (#80446).
err.span_suggestion_verbose(
lhs_expr.span.shrink_to_hi(),
"you might have meant to write a semicolon here",
";".to_string(),
Applicability::MaybeIncorrect,
);
// We defer to the later error produced by `check_lhs_assignable`.
err.delay_as_bug();
}
let suggest_deref_binop =

View File

@ -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}");
}

View File

@ -1,9 +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
//~^ ERROR cannot multiply `()` by `&mut {integer}`
let _ = x;
println!("{y}");
}

View File

@ -1,18 +1,5 @@
error[E0369]: cannot multiply `()` by `&mut {integer}`
--> $DIR/false-binop-caused-by-missing-semi.rs:6:5
|
LL | foo()
| ----- ()
LL | *x = 0;
| ^- &mut {integer}
|
help: you might have meant to write a semicolon here
|
LL | foo();
| +
error[E0070]: invalid left-hand side of assignment
--> $DIR/false-binop-caused-by-missing-semi.rs:6:8
--> $DIR/false-binop-caused-by-missing-semi.rs:7:8
|
LL | / foo()
LL | | *x = 0;
@ -25,7 +12,6 @@ help: you might have meant to write a semicolon here
LL | foo();
| +
error: aborting due to 2 previous errors
error: aborting due to previous error
Some errors have detailed explanations: E0070, E0369.
For more information about an error, try `rustc --explain E0070`.
For more information about this error, try `rustc --explain E0070`.