rust-lang / rust-lang/rustfmt

Allow manually-specifying which macros should be formatted and how

Open
#3,705 6 comments 23 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

The libc crate has some macros, called s!, f!, etc. which take a series of items:

f! {
    fn foo();
    fn bar();
    // ...
}

I know that formatting macros is very tricky, and that we probably cannot do that by default.

Instead, would it be possible to specify, e.g., in the rustfmt.toml, a list of macros for which rustfmt should try to format the contents as if the contents were a sequence of items ?

E.g. for the case above, rustfmt could format the f! { ... } the contents of ... as if they were items, adding 4 space indentation inside the block.

I think approaches to macros in which users opt-into formatting them, and somehow conveying what the content of the macro is, might be a more viable way forward with respect to macro formatting than trying to do things automatically.

I don't know how to call this, but I could add the following to libc's rustfmt.toml:

format-macro-content-as-items = ["f", "s"]

I think I could use this in other crates that also make heavy use of macros, like packed-simd.

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 by reviewing the existing macro-formatting behavior and configuration in rustfmt, then consider how rustfmt.toml could represent macros whose contents should be treated as items. Compare the proposed behavior using the f! and s! examples; done would mean an agreed configuration design and formatting behavior for opted-in macros.

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
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.