rust-lang / rust-lang/rustfmt

`rustfmt` needs more than 1 run to reach an idempotent state in some cases

Open
#5,824 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Consider e.g. the following code:

#[derive(Clone, Debug)]

#[derive(Default, Copy, PartialEq, PartialOrd)]
pub struct Vec2D(pub [f32; 2usize]);

fn main() {}

Running cargo fmt once will get rid of the superfluous linefeed as you'd expect:

#[derive(Clone, Debug)]
#[derive(Default, Copy, PartialEq, PartialOrd)]
pub struct Vec2D(pub [f32; 2usize]);

fn main() {}

But since rustfmt doesn't seem to recurse, it doesn't notice that there's an opportunity to merge the two #derive clauses here, which leaves us in an awkward situation where things aren't quite fully formatted.

Running cargo fmt once more will merge the two #derive clauses and actually put us in an idempotent state this time:

#[derive(Clone, Debug, Default, Copy, PartialEq, PartialOrd)]
pub struct Vec2D(pub [f32; 2usize]);

fn main() {}

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

Start by reproducing the example with rustfmt or cargo fmt and compare the first and second formatting runs. Trace the formatting path responsible for removing blank lines and merging adjacent derive clauses, then add coverage showing that one run reaches the final idempotent output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.