Skip to content

Commit

Permalink
Better account for else if macro conditions mising an if
Browse files Browse the repository at this point in the history
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! {} {
   |            ++
```
  • Loading branch information
estebank committed Nov 16, 2024
1 parent 04fe839 commit 629a69f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,13 @@ impl<'a> Parser<'a> {
// ^^
// }
//
// We account for macro calls that were meant as conditions as well.
//
// if ... {
// } else if macro! { foo bar } {
// ^^
// }
//
// If $cond is "statement-like" such as ExprKind::While then we
// want to suggest wrapping in braces.
//
Expand All @@ -2693,7 +2700,9 @@ impl<'a> Parser<'a> {
// }
// ^
if self.check(&TokenKind::OpenDelim(Delimiter::Brace))
&& classify::expr_requires_semi_to_be_stmt(&cond) =>
&& (classify::expr_requires_semi_to_be_stmt(&cond)
|| matches!(cond.kind, ExprKind::MacCall(..)))
=>
{
self.dcx().emit_err(errors::ExpectedElseBlock {
first_tok_span,
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/parser/else-no-if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ error: expected `{`, found `falsy`
--> $DIR/else-no-if.rs:47:12
|
LL | } else falsy! {} {
| ^^^^^ expected `{`
| ---- ^^^^^
| |
| 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! {} {
| ++

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

0 comments on commit 629a69f

Please sign in to comment.