rust-lang / rust-lang/rustfmt

Format macro calls (not definitions)

Open
#2,905 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hello,

I haven't been able to find this in the issue tracker. If it is there, sorry for missing it!

On IRC, someone wanted to auto-format a big match-like macro, like:

match_any_type!(var, {
    Foo::A => a,
    Foo::B => {
        bar();
        b
    }
    // etc.
})

The use case appears to make sense to me, but I don't think any rust formatting tool is currently able to handle this, as it is completely macro-dependent.

However, @thepowersgang proposed a solution:

Mutabah | [snip] Re getting auto-formatting of macro input - I don't know if there's formatting tools that can do that... a
        | powerful one could possibly look at the macro signature and guess at formatting for fragements                      

IOW, rustfmt could (theoretically) check the macro signature and figure out what parts are of what type, to then try to indent them correctly.

I don't know if this is actually doable within reasonable complexity, but thought this idea was great and deserved to be tracked :)

Anyway, thank you for your work on rustfmt!

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

No files, tests, or entry points are named. Start by assessing whether rustfmt can inspect macro signatures and infer fragment types for macro calls; done would require a defined, feasible approach to formatting macro input rather than only macro definitions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.