`struct_field_align_threshold` ignores inline comments before struct literal fields

Aperta
#7,204 4 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@tkr-sh ci sta già lavorando.

Dal 24/10/2026.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
devtools, tooling

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Rust
Stelle
7.1k
Fork
1.2k
Merge medio
1g 4h
PR unite (30g)
20

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rustfmt

Tutte le issue di rust-lang/rustfmt

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.