rust-lang / rust-lang/rustfmt

file-lines can't format trait definition contents without formatting the header

Open
#6,885 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When selecting the contents of a trait definition with --file-lines, everything up to the first function definition is formatted, even when not selected. This means it's not possible to e.g. format only one function in the trait definition without touching the header portion. In this case "header" means everything from the trait keyword to the fn keyword of the first function defined in the trait (or whatever the first item in the trait is).

Summary

For the following code in test.rs:

trait WonkyTest
{
fn required_1(
arg: bool,
arg2: i32,
);
fn required_2(
arg: bool,
arg2: i32,
);
}

I would like to use --file-lines to format only lines 7-10 (just the definition of required_2), and would expect that nothing else outside of the selected lines would be changed.

Expected behavior
trait WonkyTest
{
fn required_1(
arg: bool,
arg2: i32,
);
    fn required_2(arg: bool, arg2: i32);
}
Actual behavior
trait WonkyTest {
    fn required_1(
arg: bool,
arg2: i32,
);
    fn required_2(arg: bool, arg2: i32);
}

Note that the opening { was moved and that whitespace was added before the start of fn required_1.

Configuration

rustfmt cli options used:

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

Meta

This likely needs to be added as a blocker for https://github.com/rust-lang/rustfmt/issues/3397.

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.

Research direction

Reproduce the issue with the provided test.rs example and the rustfmt --unstable-features --file-lines command selecting lines 7-10. Trace the --file-lines handling for trait definitions, using issue #3397 as related context; done means only required_2 is reformatted while the trait header and required_1 remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.