`--file-lines` statement preservation can result in poorly formatted code
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Start by reproducing with the exact command in the report: create test.rs with the sample code and run cargo run --bin rustfmt -- --unstable-features --file-lines '[{"file":"test.rs","range":[2,2]}]' test.rs. Then read PR #6867, which added the statement-preservation logic (including preserved leading whitespace), to find where unselected statements are emitted unchanged while their enclosing block is re-indented. Done means the partial-format output keeps preserved statements aligned with the new indentation of the block, matching whole-file formatting of the surrounding lines, with a regression test in the rustfmt test suite for --file-lines.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
https://github.com/rust-lang/rustfmt/pull/6867 introduced logic for preserving statements not covered by the --file-lines selection, including preserving the leading whitespace. This can result in very poorly formatted code when the block containing the statement changes indentation while the statement stays in place.
Summary
When using --file-lines to partially format a file, it's possible that a partially-selected block gets rewritten while its contents do not. This results in very weirdly formatted code that feels incorrect. This happens primarily when the indentation of a block or closure changes while the contents of that block are unselected. This leaves unselected statements within the block at an incorrect indentation compared to their new surroundings.
Consider the following code:
fn main() {
call(super_long_argument_that_forces_vertical_layout_______________________________, || {
statement;
});
}
Let's say I've changed the first argument of the call, so I want to format the file with line 2 selected.
Expected behavior
I'd expect to get something that's fairly consistent with what whole-file formatting produces:
fn main() {
call(
super_long_argument_that_forces_vertical_layout_______________________________,
|| {
statement;
},
);
}
Actual behavior
The unselected statement and the closing brace of the closure end up keeping their indentation, resulting in messy code:
fn main() {
call(
super_long_argument_that_forces_vertical_layout_______________________________,
|| {
statement;
},
);
}
Configuration
rustfmt cli options used (if applicable):
cargo run --bin rustfmt -- --unstable-features --file-lines '[{"file":"test.rs","range":[2,2]}]' test.rs
Meta
Tested against main commit de3a4b0c3f93e544eb4c22bba905a1c8a4e5067f.
- 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
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rust-lang/rustfmt
-
A-config C-bug SO-print-config
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
-
A-Unicode C-bug P-low
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
rust-lang/rustfmt#7210 · 5 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
A-imports C-bug E-hard I-non-idempotency X-impacts-stable-default-format
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
rust-lang/rustfmt#7173 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
A-comments C-bug only-with-option UO-indent_style
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Fix `first_line_width` to be tab-awareCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởA-tabs A-whitespace C-bug SO-hard_tabs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của rust-lang/rustfmt
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
tamatebako/tebako#1155 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
swc-project/swc-next-issues#1148 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
registrystack/registry-stack#2828 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
agent:triaged arch-audit bug priority:p1
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
SocketDev/socket-patch#2364 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày