Rollup merge of #93574 - compiler-errors:bad-let-suggestion, r=lcnr

don't suggest adding `let` due to bad assignment expressions inside of `while` loop

adds a check that our `lhs` expression is actually within the conditional part of the `while` loop, instead of anywhere in the `while` body.

fixes #93486
This commit is contained in:
Matthias Krüger 2022-02-02 19:34:06 +01:00 committed by GitHub
commit 799bded9b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 8 deletions

View File

@ -865,14 +865,22 @@ pub(crate) fn check_lhs_assignable(
),
..
}) => {
// We have a situation like `while Some(0) = value.get(0) {`, where `while let`
// was more likely intended.
err.span_suggestion_verbose(
expr.span.shrink_to_lo(),
"you might have meant to use pattern destructuring",
"let ".to_string(),
Applicability::MachineApplicable,
);
// Check if our lhs is a child of the condition of a while loop
let expr_is_ancestor = std::iter::successors(Some(lhs.hir_id), |id| {
self.tcx.hir().find_parent_node(*id)
})
.take_while(|id| *id != parent)
.any(|id| id == expr.hir_id);
// if it is, then we have a situation like `while Some(0) = value.get(0) {`,
// where `while let` was more likely intended.
if expr_is_ancestor {
err.span_suggestion_verbose(
expr.span.shrink_to_lo(),
"you might have meant to use pattern destructuring",
"let ".to_string(),
Applicability::MachineApplicable,
);
}
break;
}
hir::Node::Item(_)

View File

@ -0,0 +1,6 @@
fn main() {
while let 1 = 1 {
vec![].last_mut().unwrap() = 3_u8;
//~^ ERROR invalid left-hand side of assignment
}
}

View File

@ -0,0 +1,11 @@
error[E0070]: invalid left-hand side of assignment
--> $DIR/issue-93486.rs:3:36
|
LL | vec![].last_mut().unwrap() = 3_u8;
| -------------------------- ^
| |
| cannot assign to this expression
error: aborting due to previous error
For more information about this error, try `rustc --explain E0070`.