Skip to content

Commit 4c4e3ef

Browse files
Merge pull request #159 from calebcartwright/match-rhs-mac-call-exprs
clarify match block requirement for single mac call
2 parents c923196 + f72c79a commit 4c4e3ef

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

guide/expressions.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ never use a block (unless the block is empty).
634634

635635
If the right-hand side consists of multiple statements or has line comments or
636636
the start of the line cannot be fit on the same line as the left-hand side, use
637-
a block.
637+
a block. A block may also be used in cases where the right-hand side is a macro call expression to prevent issues with expansions containing a trailing semicolon, more details [below](#macro-call-expressions).
638638

639639
The body of a block arm should be block indented once.
640640

@@ -661,7 +661,7 @@ match foo {
661661
```
662662

663663
If the body is a single expression with no line comments and not a control flow
664-
expression, then it may be started on the same line as the right-hand side. If
664+
expression, then it may be started on the same line as the left-hand side. If
665665
not, then it must be in a block. Example,
666666

667667
```rust
@@ -777,6 +777,46 @@ We define a pattern clause to be *small* if it matches the following grammar:
777777

778778
E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
779779

780+
#### Macro call expressions
781+
When the right-hand side of a match arm contains a macro call expression, it may be necessary to use a block to prevent issues in expansion.
782+
783+
In some cases the right-hand side may be placed on the same line as the left-hand side. E.g.,
784+
785+
```rust
786+
macro_rules! expr {
787+
() => {
788+
true
789+
};
790+
}
791+
792+
fn main() {
793+
let _val: bool = match true {
794+
true => expr!(),
795+
false => false,
796+
};
797+
}
798+
```
799+
800+
However, in other cases it is necessary to use a block to prevent issues in macro expansion, such as with trailing semicolons.
801+
802+
```rust
803+
macro_rules! stmt {
804+
() => {
805+
true;
806+
};
807+
}
808+
809+
fn main() {
810+
match true {
811+
true => {
812+
stmt!()
813+
}
814+
false => {}
815+
}
816+
}
817+
```
818+
819+
Note that at the time of this writing [rustc ignores these trailing semicolons](https://github.com/rust-lang/rust/issues/33953), but this guidance is provided in case that changes.
780820

781821
### Combinable expressions
782822

0 commit comments

Comments
 (0)