rust-lang / rust-lang/rustfmt

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

Open
#7,071 2 comments 0 reactions 1 assignee View on GitHub

@saberoueslati is already working on this.

Since Aug 29, 2026.

C-bug I-trailing-whitespace UO-error_on_unformatted UO-file_lines
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.