about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-01-03 14:44:18 +0100
committerGitHub <noreply@github.com>2022-01-03 14:44:18 +0100
commit0335b7bca935efa2b25a6996f6209f7e8c2c4713 (patch)
tree45e05c0631c0ebf95be54a6c6a56a11521eb0419
parentfd09f342f34b27d2439576d1ab681d42b2b8fac8 (diff)
parent874cd08e230f38e9640f995c0286c540a3a9a60d (diff)
downloadrust-0335b7bca935efa2b25a6996f6209f7e8c2c4713.tar.gz
rust-0335b7bca935efa2b25a6996f6209f7e8c2c4713.zip
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.
-rw-r--r--compiler/rustc_resolve/src/late.rs2
-rw-r--r--src/test/ui/suggestions/while-let-typo.rs9
-rw-r--r--src/test/ui/suggestions/while-let-typo.stderr45
3 files changed, 56 insertions, 0 deletions
diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs
index 5098cef11e8..57305023138 100644
--- a/compiler/rustc_resolve/src/late.rs
+++ b/compiler/rustc_resolve/src/late.rs
@@ -2379,7 +2379,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);
                     })
                 });
diff --git a/src/test/ui/suggestions/while-let-typo.rs b/src/test/ui/suggestions/while-let-typo.rs
new file mode 100644
index 00000000000..dbbcdee3c19
--- /dev/null
+++ b/src/test/ui/suggestions/while-let-typo.rs
@@ -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
+}
diff --git a/src/test/ui/suggestions/while-let-typo.stderr b/src/test/ui/suggestions/while-let-typo.stderr
new file mode 100644
index 00000000000..7cc2ed3149b
--- /dev/null
+++ b/src/test/ui/suggestions/while-let-typo.stderr
@@ -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`.