Skip to content

rustfmt erroneously removes brackets around statement arg in macro #5680

Open
@ColonelThirtyTwo

Description

@ColonelThirtyTwo

Example code:

macro_rules! unwrap_or {
    ($v:expr, $s:stmt) => {
        match $v {
            Some(v) => v,
            None => { $s },
        }
    };
}

fn main() {
    let _asdf = unwrap_or!(Some(123), return);
}

Compiles fine. Running rustfmt 1.5.2-nightly (1e225413 2023-01-28) removes the braces around $s, as such:

macro_rules! unwrap_or {
    ($v:expr, $s:stmt) => {
        match $v {
            Some(v) => v,
            None => $s,
        }
    };
}

fn main() {
    let _asdf = unwrap_or!(Some(123), return);
}

The code no longer compiles:

   Compiling playground v0.0.1 (/playground)
error: expected expression, found `return;`
  --> src/main.rs:5:21
   |
5  |             None => $s,
   |                  -- ^^ expected expression
   |                  |
   |                  while parsing the `match` arm starting here
...
11 |     let _asdf = unwrap_or!(Some(123), return);
   |                 ----------------------------- in this macro invocation
   |
   = note: this error originates in the macro `unwrap_or` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `playground` due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-macrosa-matchesmatch arms, patterns, blocks, etcbugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions