rust-lang / rust-lang/rustfmt

reorder_impl_items doesn't maintain newlines

Open
#4,882 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Reproduction information

Example code:

pub struct BeautifulStruct;

impl BeautifulStruct {

    /// Another awesome doc
    const LMAO: i32 = 42;

    /// Awesome doc
    const ROFL: i32 = 10;
}

fn main() {
    println!("Hello, world!");
}

Content of ~/.config/rusfmt/rustfmt.toml:

reorder_impl_items = true

Output of rustfmt -V: rustfmt 1.4.37-nightly (7c3872e 2021-06-24)

Problem

If I run rustfmt with the settings and the file above, rustfmt changes the file to this:

pub struct BeautifulStruct;

impl BeautifulStruct {
    /// Another awesome doc
    const LMAO: i32 = 42;
    /// Awesome doc
    const ROFL: i32 = 10;
}

fn main() {
    println!("Hello, world!");
}

But I'd expect to be like this:

pub struct BeautifulStruct;

impl BeautifulStruct {
    /// Another awesome doc
    const LMAO: i32 = 42;

    /// Awesome doc
    const ROFL: i32 = 10;
}

fn main() {
    println!("Hello, world!");
}

If you don't see the difference: There's a blank line between const LMAO: i32 = 42; and the documentation paragraph /// Awesome doc in my "expectation-example".

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 and the shown rustfmt.toml setting, focusing on reorder_impl_items and the lost blank line between the two documented constants. Done means the formatted output preserves that blank line while still reordering implementation items.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.