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:
commit
799bded9b4
@ -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(_)
|
||||
|
6
src/test/ui/typeck/issue-93486.rs
Normal file
6
src/test/ui/typeck/issue-93486.rs
Normal file
@ -0,0 +1,6 @@
|
||||
fn main() {
|
||||
while let 1 = 1 {
|
||||
vec![].last_mut().unwrap() = 3_u8;
|
||||
//~^ ERROR invalid left-hand side of assignment
|
||||
}
|
||||
}
|
11
src/test/ui/typeck/issue-93486.stderr
Normal file
11
src/test/ui/typeck/issue-93486.stderr
Normal 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`.
|
Loading…
Reference in New Issue
Block a user