`struct_field_align_threshold` ignores inline comments before struct literal fields

Đang mở
#7,204 4 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@tkr-sh đang làm issue này rồi.

Từ ngày 24/10/2026.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
devtools, tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Rust
Star
7.1k
Fork
1.2k
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
20

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/rustfmt

Tất cả issue của rust-lang/rustfmt

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.