Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarify match block requirement for single mac call #159

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions guide/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ never use a block (unless the block is empty).

If the right-hand side consists of multiple statements or has line comments or
the start of the line cannot be fit on the same line as the left-hand side, use
a block.
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).

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

Expand All @@ -661,7 +661,7 @@ match foo {
```

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

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

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

#### Macro call expressions
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.

In some cases the right-hand side may be placed on the same line as the left-hand side. E.g.,

```rust
macro_rules! expr {
() => {
true
};
}

fn main() {
let _val: bool = match true {
true => expr!(),
false => false,
};
}
```

However, in other cases it is necessary to use a block to prevent issues in macro expansion, such as with trailing semicolons.

```rust
macro_rules! stmt {
() => {
true;
};
}

fn main() {
match true {
true => {
stmt!()
}
false => {}
}
}
```

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.

### Combinable expressions

Expand Down