From a7e32a5319f82c19f7c8024efd3afcc1825fa5e2 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 31 Aug 2025 00:42:58 +0000 Subject: Provide suggestion when encountering `match () { () => 1 } + match () { () => 1 }` ``` error[E0308]: mismatched types --> $DIR/expr-as-stmt.rs:69:5 | LL | match () { () => 1 } + match () { () => 1 } | ^^^^^^^^^^^^^^^^^^^^ expected `()`, found integer | help: consider using a semicolon here | LL | match () { () => 1 }; + match () { () => 1 } | + help: alternatively, parentheses are required to parse this as an expression | LL | (match () { () => 1 }) + match () { () => 1 } | + + ``` Parentheses are needed for the `match` to be unambiguously parsed as an expression and not a statement when chaining with binops that are also unops. --- tests/ui/parser/expr-as-stmt.stderr | 13 ++++++++++--- tests/ui/suggestions/match-needing-semi.stderr | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/ui/parser/expr-as-stmt.stderr b/tests/ui/parser/expr-as-stmt.stderr index 577c3455a71..562162ef394 100644 --- a/tests/ui/parser/expr-as-stmt.stderr +++ b/tests/ui/parser/expr-as-stmt.stderr @@ -227,9 +227,16 @@ error[E0308]: mismatched types --> $DIR/expr-as-stmt.rs:69:5 | LL | match () { () => 1 } + match () { () => 1 } - | ^^^^^^^^^^^^^^^^^^^^- help: consider using a semicolon here - | | - | expected `()`, found integer + | ^^^^^^^^^^^^^^^^^^^^ expected `()`, found integer + | +help: consider using a semicolon here + | +LL | match () { () => 1 }; + match () { () => 1 } + | + +help: alternatively, parentheses are required to parse this as an expression + | +LL | (match () { () => 1 }) + match () { () => 1 } + | + + error[E0308]: mismatched types --> $DIR/expr-as-stmt.rs:75:14 diff --git a/tests/ui/suggestions/match-needing-semi.stderr b/tests/ui/suggestions/match-needing-semi.stderr index b5f01d7038c..8f74997fdbe 100644 --- a/tests/ui/suggestions/match-needing-semi.stderr +++ b/tests/ui/suggestions/match-needing-semi.stderr @@ -28,9 +28,20 @@ LL | | 4 => 1, LL | | 3 => 2, LL | | _ => 2 LL | | } - | | ^- help: consider using a semicolon here - | |_____| - | expected `()`, found integer + | |_____^ expected `()`, found integer + | +help: consider using a semicolon here + | +LL | }; + | + +help: alternatively, parentheses are required to parse this as an expression + | +LL ~ (match 3 { +LL | 4 => 1, +LL | 3 => 2, +LL | _ => 2 +LL ~ }) + | error: aborting due to 2 previous errors -- cgit 1.4.1-3-g733a5