MacroArgParser
Discards Spaces After Commas Sometimes
#5573
Labels
MacroArgParser
Discards Spaces After Commas Sometimes
#5573
Example
This is the minimal example of the issue I was running into, but some of the tests (talked about below),
also do this, despite having 2 meta-variables.
Version
rustfmt 1.5.1-nightly (a24a020e 2022-10-18)
Running on Windows 10 (21H2)
Discussion
There are tests that validate that spaces after commas ARE removed. This line:
rustfmt/tests/source/macro_rules.rs
Line 30 in ef91154
rustfmt/tests/target/macro_rules.rs
Lines 45 to 49 in ef91154
the test was added in #2542 "Put spaces around braces", and this is completely unrelated to brace spacing. Additionally, the 'source' does have the space in it.
All the tests affected by this weird formatting were added in the commit.
Either way, not having a space after the comma looks pretty weird to me.
Everywhere else in Rust I'm aware of, commas should be followed by whitespace (or a newline).
Proposed Solution
I wanted to make sure this was an issue before opening a PR, but I've fixed this locally.
The
MacroArgParser
callsnext_space
to determine whether to place a space:rustfmt/src/macros.rs
Lines 1055 to 1079 in ef91154
TokenKind::Comma
from the match it will fall into the default case.So it returns
Always
instead ofPunctuation
, so a space is always placed after it.I think this makes sense compared to the other punctuation in the list,
since it's pretty standard to place a space after commas.
Honestly, this fix feels deceptively simple, but all tests pass (except those mentioned above),
and everything I've thrown at my build so far seems fine.
Feel free to point me in the right direction if this fix is totally bogus though!! : v)
The text was updated successfully, but these errors were encountered: