Skip to content

Commit d74a1d4

Browse files
docs: guidance for mac calls in match arm rhs
1 parent c923196 commit d74a1d4

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

guide/expressions.md

Lines changed: 41 additions & 1 deletion
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

@@ -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)