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

Aperta
#7,071 2 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@saberoueslati ci sta già lavorando.

Dal 29/8/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

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.

Lingua principale
Rust
Stelle
7.1k
Fork
1.2k
Merge medio
1g 4h
PR unite (30g)
20

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rustfmt

Tutte le issue di rust-lang/rustfmt

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.