`--file-lines` statement preservation can result in poorly formatted code

オープン
#7,257 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
tooling

調査の方向性

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 の本文から書いたものです。

説明

A-statements C-bug C-discussion UO-file_lines

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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/rustfmt のほかの issue

rust-lang/rustfmt の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。