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

Suggest while let x = y when encountering while x = y #92402

Merged
merged 1 commit into from
Jan 3, 2022
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
2 changes: 2 additions & 0 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
ExprKind::While(ref cond, ref block, label) => {
self.with_resolved_label(label, expr.id, |this| {
this.with_rib(ValueNS, NormalRibKind, |this| {
let old = this.diagnostic_metadata.in_if_condition.replace(cond);
this.visit_expr(cond);
this.diagnostic_metadata.in_if_condition = old;
this.visit_block(block);
})
});
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/while-let-typo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
let foo = Some(0);
let bar = None;
while Some(x) = foo {} //~ ERROR cannot find value `x` in this scope
while Some(foo) = bar {}
while 3 = foo {} //~ ERROR mismatched types
while Some(3) = foo {} //~ ERROR invalid left-hand side of assignment
while x = 5 {} //~ ERROR cannot find value `x` in this scope
}
45 changes: 45 additions & 0 deletions src/test/ui/suggestions/while-let-typo.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
error[E0425]: cannot find value `x` in this scope
--> $DIR/while-let-typo.rs:4:16
|
LL | while Some(x) = foo {}
| ^ not found in this scope
|
help: you might have meant to use pattern matching
|
LL | while let Some(x) = foo {}
| +++

error[E0425]: cannot find value `x` in this scope
--> $DIR/while-let-typo.rs:8:11
|
LL | while x = 5 {}
| ^ not found in this scope
|
help: you might have meant to use pattern matching
|
LL | while let x = 5 {}
| +++

error[E0308]: mismatched types
--> $DIR/while-let-typo.rs:6:11
|
LL | while 3 = foo {}
| ^^^^^^^ expected `bool`, found `()`

error[E0070]: invalid left-hand side of assignment
--> $DIR/while-let-typo.rs:7:19
|
LL | while Some(3) = foo {}
| - ^
| |
| cannot assign to this expression
|
help: you might have meant to use pattern destructuring
|
LL | while let Some(3) = foo {}
| +++

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0070, E0308, E0425.
For more information about an error, try `rustc --explain E0070`.