Skip to content

Commit a7e32a5

Browse files
committed
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.
1 parent fe55364 commit a7e32a5

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19141914
self.check_expr_has_type_or_error(expr, self.tcx.types.unit, |err| {
19151915
if expr.can_have_side_effects() {
19161916
self.suggest_semicolon_at_end(expr.span, err);
1917+
if let hir::ExprKind::Match(..) = expr.kind {
1918+
err.multipart_suggestion(
1919+
"alternatively, parentheses are required to parse this as an \
1920+
expression",
1921+
vec![
1922+
(expr.span.shrink_to_lo(), "(".to_string()),
1923+
(expr.span.shrink_to_hi(), ")".to_string()),
1924+
],
1925+
Applicability::MaybeIncorrect,
1926+
);
1927+
}
19171928
}
19181929
});
19191930
}

tests/ui/parser/expr-as-stmt.stderr

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,16 @@ error[E0308]: mismatched types
227227
--> $DIR/expr-as-stmt.rs:69:5
228228
|
229229
LL | match () { () => 1 } + match () { () => 1 }
230-
| ^^^^^^^^^^^^^^^^^^^^- help: consider using a semicolon here
231-
| |
232-
| expected `()`, found integer
230+
| ^^^^^^^^^^^^^^^^^^^^ expected `()`, found integer
231+
|
232+
help: consider using a semicolon here
233+
|
234+
LL | match () { () => 1 }; + match () { () => 1 }
235+
| +
236+
help: alternatively, parentheses are required to parse this as an expression
237+
|
238+
LL | (match () { () => 1 }) + match () { () => 1 }
239+
| + +
233240

234241
error[E0308]: mismatched types
235242
--> $DIR/expr-as-stmt.rs:75:14

tests/ui/suggestions/match-needing-semi.stderr

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,20 @@ LL | | 4 => 1,
2828
LL | | 3 => 2,
2929
LL | | _ => 2
3030
LL | | }
31-
| | ^- help: consider using a semicolon here
32-
| |_____|
33-
| expected `()`, found integer
31+
| |_____^ expected `()`, found integer
32+
|
33+
help: consider using a semicolon here
34+
|
35+
LL | };
36+
| +
37+
help: alternatively, parentheses are required to parse this as an expression
38+
|
39+
LL ~ (match 3 {
40+
LL | 4 => 1,
41+
LL | 3 => 2,
42+
LL | _ => 2
43+
LL ~ })
44+
|
3445

3546
error: aborting due to 2 previous errors
3647

0 commit comments

Comments
 (0)