`--file-lines` statement preservation can result in poorly formatted code
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Rust
- スター
- 7.1k
- フォーク
- 1.2k
- 平均マージ
- 1日 4時間
- マージ済み PR(30日)
- 20
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rust-lang/rustfmt のほかの issue
-
A-config C-bug SO-print-config
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
A-Unicode C-bug P-low
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
rust-lang/rustfmt#7210 · コメント 5 件 ·
メンテナーはふだん 1 日以内に返信
-
A-imports C-bug E-hard I-non-idempotency X-impacts-stable-default-format
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
rust-lang/rustfmt#7173 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
A-comments C-bug only-with-option UO-indent_style
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Fix `first_line_width` to be tab-aware再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンA-tabs A-whitespace C-bug SO-hard_tabs
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
rust-lang/rustfmt の issue をすべて見る
似ている issue
-
bug P2
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
bug core-lang
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
Windows microphone listing treats words in a device name as DirectShow labels対応中かも @Sujal-chaudhary が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
clipy-recorder/clipy#447 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
nightly-cleanup
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
max-sixty/worktrunk#4939 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信