about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-08-31 03:00:54 +0000
committerbors <bors@rust-lang.org>2025-08-31 03:00:54 +0000
commit64a99db105f45ea3304732ffb51066c3b5193bc7 (patch)
treea731ff2123ba6ff288f43d842c9724e38ed6d4cf /tests
parentcd60c60d9f6bf13ca96ecde7392327c3caf6f162 (diff)
parent3af81cf0b7bd394dac89cbacec303b5937b5519a (diff)
downloadrust-64a99db105f45ea3304732ffb51066c3b5193bc7.tar.gz
rust-64a99db105f45ea3304732ffb51066c3b5193bc7.zip
Auto merge of #145582 - estebank:issue-107806, r=chenyukang
Detect missing `if let` or `let-else`

During `let` binding parse error and encountering a block, detect if there is a likely missing `if` or `else`:

```
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
  --> $DIR/missing-if-let-or-let-else.rs:14:25
   |
LL |     let Some(x) = foo() {
   |                         ^ expected one of `.`, `;`, `?`, `else`, or an operator
   |
help: you might have meant to use `if let`
   |
LL |     if let Some(x) = foo() {
   |     ++
help: alternatively, you might have meant to use `let else`
   |
LL |     let Some(x) = foo() else {
   |                         ++++
```

Fix rust-lang/rust#107806.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/uninhabited/missing-if-let-or-let-else.rs24
-rw-r--r--tests/ui/uninhabited/missing-if-let-or-let-else.stderr39
2 files changed, 63 insertions, 0 deletions
diff --git a/tests/ui/uninhabited/missing-if-let-or-let-else.rs b/tests/ui/uninhabited/missing-if-let-or-let-else.rs
new file mode 100644
index 00000000000..51fedb79756
--- /dev/null
+++ b/tests/ui/uninhabited/missing-if-let-or-let-else.rs
@@ -0,0 +1,24 @@
+fn a() {
+    let Some(x) = foo() { //~ ERROR expected one of
+        //~^ HELP you might have meant to use `if let`
+        let y = x;
+    }
+}
+fn b() {
+    let Some(x) = foo() { //~ ERROR expected one of
+        //~^ HELP you might have meant to use `let else`
+        return;
+    }
+}
+fn c() {
+    let Some(x) = foo() { //~ ERROR expected one of
+        //~^ HELP you might have meant to use `if let`
+        //~| HELP alternatively, you might have meant to use `let else`
+        // The parser check happens pre-macro-expansion, so we don't know for sure.
+        println!("{x}");
+    }
+}
+fn foo() -> Option<i32> {
+    Some(42)
+}
+fn main() {}
diff --git a/tests/ui/uninhabited/missing-if-let-or-let-else.stderr b/tests/ui/uninhabited/missing-if-let-or-let-else.stderr
new file mode 100644
index 00000000000..4b78a0fa16e
--- /dev/null
+++ b/tests/ui/uninhabited/missing-if-let-or-let-else.stderr
@@ -0,0 +1,39 @@
+error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
+  --> $DIR/missing-if-let-or-let-else.rs:2:25
+   |
+LL |     let Some(x) = foo() {
+   |                         ^ expected one of `.`, `;`, `?`, `else`, or an operator
+   |
+help: you might have meant to use `if let`
+   |
+LL |     if let Some(x) = foo() {
+   |     ++
+
+error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
+  --> $DIR/missing-if-let-or-let-else.rs:8:25
+   |
+LL |     let Some(x) = foo() {
+   |                         ^ expected one of `.`, `;`, `?`, `else`, or an operator
+   |
+help: you might have meant to use `let else`
+   |
+LL |     let Some(x) = foo() else {
+   |                         ++++
+
+error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
+  --> $DIR/missing-if-let-or-let-else.rs:14:25
+   |
+LL |     let Some(x) = foo() {
+   |                         ^ expected one of `.`, `;`, `?`, `else`, or an operator
+   |
+help: you might have meant to use `if let`
+   |
+LL |     if let Some(x) = foo() {
+   |     ++
+help: alternatively, you might have meant to use `let else`
+   |
+LL |     let Some(x) = foo() else {
+   |                         ++++
+
+error: aborting due to 3 previous errors
+