rust-lang / rust-lang/rustfmt

Formatting a macro that generates a struct with a field with a multiline attribute adds indentation forever

Open
#5,489 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

macro_rules! generate_the_struct {
    ($a:literal) => {
        pub struct Struct {
            #[clap(
                long = "--alpha-beta-gamma",
                env = "ALPHA_BETA_GAMMA",
                default_value = $a,
            )]
            alpha_beta_gamma: usize,
        }
    };
}

The arguments of the clap attribute will be indented for every call to cargo fmt. For example...

% for x in $(seq 10); do cargo fmt; done
% git diff
diff --git a/src/main.rs b/src/main.rs
index 1f1a25c..c1dee62 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,10 +2,10 @@ macro_rules! generate_the_struct {
     ($a:literal) => {
         pub struct Struct {
             #[clap(
-                long = "--alpha-beta-gamma",
-                env = "ALPHA_BETA_GAMMA",
-                default_value = $a,
-            )]
+                                                                        long = "--alpha-beta-gamma",
+                                                                        env = "ALPHA_BETA_GAMMA",
+                                                                        default_value = $a,
+                                                                    )]
             alpha_beta_gamma: usize,
         }
     };

This occurs with:

  • rustfmt 1.4.38-stable (e092d0b6 2022-07-16)
  • rustfmt 1.5.1-nightly (f6f9d5e7 2022-08-04)

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 reproducing the issue with the macro example and repeated cargo fmt runs, then inspect the rustfmt formatting path for macro-generated structs and multiline attributes. Done means formatting the same input repeatedly produces identical indentation and the supplied example no longer grows on each run.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.