rust-lang / rust-lang/rustfmt

`MacroArgParser` Discards Spaces After Commas Sometimes

Open
#5,573 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros I-poor-formatting only-with-option
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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.

 macro_rules! test_macro {
-    ($child:expr, Module) => {
+    ($child:expr,Module) => {
         let x = 11;
     };
 }

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:
https://github.com/rust-lang/rustfmt/blob/ef91154250977b3b5d05448dafbca524a1168b47/tests/source/macro_rules.rs#L30 gets reformatted to: https://github.com/rust-lang/rustfmt/blob/ef91154250977b3b5d05448dafbca524a1168b47/tests/target/macro_rules.rs#L45-L49 But this seems suspicious,
the test was added in https://github.com/rust-lang/rustfmt/pull/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 calls next_space to determine whether to place a space:
https://github.com/rust-lang/rustfmt/blob/ef91154250977b3b5d05448dafbca524a1168b47/src/macros.rs#L1055-L1079 By removing TokenKind::Comma from the match it will fall into the default case.
So it returns Always instead of Punctuation, 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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/macros.rs at MacroArgParser::next_space, where the issue identifies the comma-specific spacing behavior. Review the related cases in tests/source/macro_rules.rs and tests/target/macro_rules.rs, then verify that comma-separated macro arguments retain a space and that the affected formatting tests reflect the intended output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.