--file-lines can trip up trailing whitespace validity check

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

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

@saberoueslati がすでに取り組んでいます。

2026年8月29日 から。

評価

この issue はまだ評価されていません。

説明

C-bug I-trailing-whitespace only-with-option UO-error_on_unformatted UO-file_lines

rustfmt has a post-formatting validity check that looks for trailing whitespace that was left behind after formatting, and it will output errors if trailing whitespace was leftover. This will also cause tests to fail if detected in test output. This validity check is not handled correctly in combination with the --file-lines feature, which can cause the validity check to fail on unselected lines.

Summary

Consider the following Rust code:

fn foo(
    first: i32, // force vertical



    selected: i32,
) {
trailing();   
}

Note that there is trailing whitespace on line 8.

I can use --file-lines to format just the arguments of the function (lines 2-6). If I do so, rustfmt correctly formats the arguments without touching the body of the function, but it incorrectly emits an error in the process.

Expected behavior

Formats the selected lines without error.

Actual behavior

Formats the selected lines, but produces an error about trailing whitespace on unselected lines:

error[internal]: left behind trailing whitespace
 --> /home/legare/rustfmt/test.rs:6:6:12
  |
6 | trailing();   
  |            ^^^
  |

warning: rustfmt has failed to format. See previous 1 errors.

Configuration

rustfmt cli options used:

rustfmt --unstable-features --file-lines '[{"file":"test.rs","range":[2,6]}]' test.rs

Meta

The issue seems to be that the post-formatting trailing whitespace check is looking at the lines specified by --file-lines without taking into account that the selected lines may have changed in length. In this case, lines 2-6 map to lines 2-4 in the formatted output, and the line with trailing whitespace gets moved to line 6. The validity check then looks for trailing whitespace on lines 2-6, even though lines 5-6 of the output do not correspond to lines selected by --file-lines, and so were not formatted.

主要言語
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 を短くまとめたダイジェスト。