Skip to content

Commit 0335b7b

Browse files
authored
Rollup merge of #92402 - pr2502:while-let-typo, r=oli-obk
Suggest while let x = y when encountering while x = y Extends #75931 to also detect where the `let` might be missing from `while let` expressions.
2 parents fd09f34 + 874cd08 commit 0335b7b

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

compiler/rustc_resolve/src/late.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
23792379
ExprKind::While(ref cond, ref block, label) => {
23802380
self.with_resolved_label(label, expr.id, |this| {
23812381
this.with_rib(ValueNS, NormalRibKind, |this| {
2382+
let old = this.diagnostic_metadata.in_if_condition.replace(cond);
23822383
this.visit_expr(cond);
2384+
this.diagnostic_metadata.in_if_condition = old;
23832385
this.visit_block(block);
23842386
})
23852387
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let foo = Some(0);
3+
let bar = None;
4+
while Some(x) = foo {} //~ ERROR cannot find value `x` in this scope
5+
while Some(foo) = bar {}
6+
while 3 = foo {} //~ ERROR mismatched types
7+
while Some(3) = foo {} //~ ERROR invalid left-hand side of assignment
8+
while x = 5 {} //~ ERROR cannot find value `x` in this scope
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0425]: cannot find value `x` in this scope
2+
--> $DIR/while-let-typo.rs:4:16
3+
|
4+
LL | while Some(x) = foo {}
5+
| ^ not found in this scope
6+
|
7+
help: you might have meant to use pattern matching
8+
|
9+
LL | while let Some(x) = foo {}
10+
| +++
11+
12+
error[E0425]: cannot find value `x` in this scope
13+
--> $DIR/while-let-typo.rs:8:11
14+
|
15+
LL | while x = 5 {}
16+
| ^ not found in this scope
17+
|
18+
help: you might have meant to use pattern matching
19+
|
20+
LL | while let x = 5 {}
21+
| +++
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/while-let-typo.rs:6:11
25+
|
26+
LL | while 3 = foo {}
27+
| ^^^^^^^ expected `bool`, found `()`
28+
29+
error[E0070]: invalid left-hand side of assignment
30+
--> $DIR/while-let-typo.rs:7:19
31+
|
32+
LL | while Some(3) = foo {}
33+
| - ^
34+
| |
35+
| cannot assign to this expression
36+
|
37+
help: you might have meant to use pattern destructuring
38+
|
39+
LL | while let Some(3) = foo {}
40+
| +++
41+
42+
error: aborting due to 4 previous errors
43+
44+
Some errors have detailed explanations: E0070, E0308, E0425.
45+
For more information about an error, try `rustc --explain E0070`.

0 commit comments

Comments
 (0)