`struct_field_align_threshold` ignores inline comments before struct literal fields

Ouverte
#7,204 4 commentaires 0 réactions 1 personne assignée Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@tkr-sh y travaille déjà.

Depuis le 24/10/2026.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
48/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
Active
Stack technique
rust
Domaine
devtools, tooling

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Langage dominant
Rust
Étoiles
7.1k
Forks
1.2k
Merge moyen
1 j 4 h
PR mergées (30 j)
20

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de rust-lang/rustfmt

Toutes les issues de rust-lang/rustfmt

Issues similaires

Plus d'issues Rust

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.