Skip to content

struct_field_align_threshold ignores inline comments before struct literal fields #7204

Open

Description

@tkr-sh

Hello,

This is a separate issue from #7203 that I noticed while working on #7202

With struct_field_align_threshold, a block comment before a struct literal field can be kept on the same line as the field name, but its width is not included in the alignment calculation, so the values do not line up.

so

Self {
    a: 1,
    /* a comment */ longer: 2,
}

gives

Self {
    a:      1,
    /* a comment */ longer: 2,
}

I think that this is kind of confusing because it doesn't align the field values to the same column, while at the same time, kind of "aligning" fields:

        a:      1,

For me there are 5 solutions

Option A: align to total prefix length

Self {
    a:                      1,
    /* a comment */ longer: 2,
}

Option B: put block comment on previous line

Self {
    a:      1,
    /* a comment */
    longer: 2,
}

Option C: refuse to format using struct_field_align_threshold

Self {
    a: 1,
    /* a comment */ longer: 2,
}

Option D: current behavior

Self {
    a:      1,
    /* a comment */ longer: 2,
}

Option E: delete comment

Self {
    a:      1,
    longer: 2,
}

which is the actual behaviour of:

Self {
    a:      1,
    longer/* a comment */: /* a comment */ 2,
}

Personally, I find Option A to be the most coherent one. If the user wants to have the block comment above because they find that the alignment takes too much width, they can just do themselves manually:

-   /* a comment */ longer: 2,
+   /* a comment */
+   longer: 2,

Note: The alignment logic is kind of shared with ast::FieldDef, so the underlying issue also affects struct definitions

Activity

  1. added
    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICE
    on Oct 21, 2026
  2. added
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    on Oct 21, 2026
  3. tkr-sh commented on Oct 21, 2026

    @tkr-sh
    Author

    @rustbot label +UO-struct_field_align_threshold +I-poor-formatting

  4. tkr-sh commented on Oct 21, 2026

    @tkr-sh
    Author

    @rustbot label -needs-triage

  5. removed
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    on Oct 21, 2026
  6. GauravPawar101 commented on Oct 24, 2026

    @GauravPawar101
  7. tkr-sh commented on Oct 24, 2026

    @tkr-sh
    Author

    Thanks for the interest! Since I opened this while working on #7202, I'd like to take it myself.

    @rustbot claim

    To be clear, the approach isn't decided yet, I lean toward Option A, but I'd like a maintainer to confirm first, since that's the reason #7202 was closed. I'll wait for that decision before opening a PR.

    And if a maintainer would rather someone else take it, I can hand it over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEI-poor-formattingIssue: poor formattingUO-struct_field_align_thresholdUnstable option: struct_field_align_threshold

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions