Skip to content

Commit 629a69f

Browse files
committed
Better account for else if macro conditions mising an if
If a macro statement has been parsed after `else`, suggest a missing `if`: ``` error: expected `{`, found `falsy` --> $DIR/else-no-if.rs:47:12 | LL | } else falsy! {} { | ---- ^^^^^ | | | expected an `if` or a block after this `else` | help: add an `if` if this is the condition of a chained `else if` statement | LL | } else if falsy! {} { | ++ ```
1 parent 04fe839 commit 629a69f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

compiler/rustc_parse/src/parser/expr.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,13 @@ impl<'a> Parser<'a> {
26832683
// ^^
26842684
// }
26852685
//
2686+
// We account for macro calls that were meant as conditions as well.
2687+
//
2688+
// if ... {
2689+
// } else if macro! { foo bar } {
2690+
// ^^
2691+
// }
2692+
//
26862693
// If $cond is "statement-like" such as ExprKind::While then we
26872694
// want to suggest wrapping in braces.
26882695
//
@@ -2693,7 +2700,9 @@ impl<'a> Parser<'a> {
26932700
// }
26942701
// ^
26952702
if self.check(&TokenKind::OpenDelim(Delimiter::Brace))
2696-
&& classify::expr_requires_semi_to_be_stmt(&cond) =>
2703+
&& (classify::expr_requires_semi_to_be_stmt(&cond)
2704+
|| matches!(cond.kind, ExprKind::MacCall(..)))
2705+
=>
26972706
{
26982707
self.dcx().emit_err(errors::ExpectedElseBlock {
26992708
first_tok_span,

tests/ui/parser/else-no-if.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ error: expected `{`, found `falsy`
7474
--> $DIR/else-no-if.rs:47:12
7575
|
7676
LL | } else falsy! {} {
77-
| ^^^^^ expected `{`
77+
| ---- ^^^^^
78+
| |
79+
| expected an `if` or a block after this `else`
80+
|
81+
help: add an `if` if this is the condition of a chained `else if` statement
82+
|
83+
LL | } else if falsy! {} {
84+
| ++
7885

7986
error: expected `{`, found `falsy`
8087
--> $DIR/else-no-if.rs:54:12

0 commit comments

Comments
 (0)