rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
5.94k stars 867 forks source link

Config option to remove spaces between braces and (a) their contents (b) macro names #6152

Open berrymot opened 4 months ago

berrymot commented 4 months ago

In non-Rust, I treat braces like other delimiters and don't put internal spaces adjacent to them:

function five() {return 5;}

It'd be nice if I could do this in Rust as well, partially because I'm using the json crate and I'm used to writing inline JSON spacelessly and would rather not add spaces to make it look consistent with the rest of rustfmt.

i.e. I'd prefer

object!{foo: 12}

but currently have to

object! { foo: 12 }
CalebLItalien commented 1 month ago

@rustbot claim

ytmimi commented 1 month ago

@berrymot for some context, rustfmt won't format the content of macro calls that use brace delimiters so if the programmer leaves leading or trailing spaces within the braces rustfmt isn't going to remove those. Additionally, back in https://github.com/rust-lang/rustfmt/pull/3177 a decision was made to always add a space after the ! when macro calls use brace delimiters since they resemble blocks.

Here are some example input and output demonstrating that rustfmt only adds a space after the ! and won't modify the content within the braces.

Input

object!{foo: 12}
object!{ foo: 12 }
object!{foo : 12}

output

object! {foo: 12}
object! { foo: 12 }
object! {foo : 12}

We could consider adding a new option to remove the space after the !, but I don't think we'd add an option to remove leading or trailing spaces within the braces.

If you're already using nightly rustfmt you could try adding the skip_macro_invocations config to your rustfmt.toml to ignore the object! calls altogether and preserve any manual formatting.

skip_macro_invocations = ["object"]