`struct_field_align_threshold` ignores inline comments before struct literal fields

Offen
#7,204 4 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@tkr-sh arbeitet bereits daran.

Seit 24.10.2026.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
48/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
rust
Bereich
devtools, tooling

Rechercherichtung

Start by reading the struct_field_align_threshold alignment logic and its shared handling of ast::FieldDef, which the issue says also affects struct definitions. Reproduce the shown struct literal case with a block comment before a field, then check how the formatter calculates the aligned value column. Done when the chosen comment behavior is consistent and covered for both struct literals and definitions.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

C-bug I-poor-formatting UO-struct_field_align_threshold

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

Vorherrschende Sprache
Rust
Sterne
7.1k
Forks
1.2k
Ø Merge
1 T. 4 Std.
Gemergte PRs (30 T.)
20

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus rust-lang/rustfmt

Alle Issues in rust-lang/rustfmt

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.