rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

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

Open berrymot opened this issue 9 months ago • 2 comments

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 }

berrymot avatar Apr 26 '24 22:04 berrymot

@rustbot claim

CalebLItalien avatar Jul 01 '24 20:07 CalebLItalien

@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"]

ytmimi avatar Jul 04 '24 02:07 ytmimi