1
Fork 0

Rollup merge of #91437 - dtolnay:emptybrace, r=nagisa

Pretty print empty blocks as {}

**Example:**

```rust
macro_rules! p {
    ($e:expr) => {
        println!("{}", stringify!($e));
    };
    ($i:item) => {
        println!("{}", stringify!($i));
    };
}

fn main() {
    p!(if true {});
    p!(struct S {});
}
```

**Before:**

```console
if true { }
struct S {
}
```

**After:**

```console
if true {}
struct S {}
```

This affects [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html), as well as ecosystem uses of stringify such as in [`anyhow::ensure!`](https://docs.rs/anyhow/1/anyhow/macro.ensure.html). Printing a `{ }` in today's heavily rustfmt'd world comes out looking jarring/sloppy.
This commit is contained in:
Matthias Krüger 2021-12-05 15:04:20 +01:00 committed by GitHub
commit a8f8f746fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
63 changed files with 182 additions and 164 deletions

View file

@ -2174,7 +2174,7 @@ impl<'a> State<'a> {
match decl.output {
hir::FnRetTy::Return(ref ty) => {
self.print_type(&ty);
self.maybe_print_comment(ty.span.lo())
self.maybe_print_comment(ty.span.lo());
}
hir::FnRetTy::DefaultReturn(..) => unreachable!(),
}
@ -2368,7 +2368,7 @@ impl<'a> State<'a> {
self.end();
if let hir::FnRetTy::Return(ref output) = decl.output {
self.maybe_print_comment(output.span.lo())
self.maybe_print_comment(output.span.lo());
}
}