`struct_field_align_threshold` ignores inline comments before struct literal fields
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
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
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
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus rust-lang/rustfmt
-
A-config C-bug SO-print-config
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
Maintainer antworten meist innerhalb von 1 Tag
-
A-Unicode C-bug P-low
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
rust-lang/rustfmt#7210 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
A-imports C-bug E-hard I-non-idempotency X-impacts-stable-default-format
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
rust-lang/rustfmt#7173 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
A-comments C-bug only-with-option UO-indent_style
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Fix `first_line_width` to be tab-awareEvtl. wieder frei Ein Pull Request dazu wurde ohne Merge geschlossen. OffenA-tabs A-whitespace C-bug SO-hard_tabs
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in rust-lang/rustfmt
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
tamatebako/tebako#1155 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
swc-project/swc-next-issues#1148 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
registrystack/registry-stack#2828 ·
Maintainer antworten meist innerhalb von 1 Tag
-
agent:triaged arch-audit bug priority:p1
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
SocketDev/socket-patch#2364 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug P2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag