rust-lang / rust-lang/rustfmt

Avoiding extra linebreaks when doc-comment added to enum

Open
#5,662 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I've noticed what I consider to be an unfortunate decision in rustfmt: the moment a single doc-comment is added in an enum, the whole enum is laid out with a line per field, even when no fields are documented.

That is, the type:

enum MyType {
    A { field1: bool, field2: bool },
    B { field1: bool, field2: bool },
    /// OMG a comment
    C { field1: bool, field2: bool },
    D { field1: bool, field2: bool },
}

becomes

enum MyType {
    A {
        field1: bool,
        field2: bool,
    },
    B {
        field1: bool,
        field2: bool,
    },
    /// OMG a comment
    C {
        field1: bool,
        field2: bool,
    },
    D {
        field1: bool,
        field2: bool,
    },
}

The formatting policy I would like would leave the type unchanged. If a doc-comment is added to a single field of a variant, then only that variant should be broken up on multiple lines rather than the whole type.

is this something that could potentially be customizable as an option in rustfmt.toml? If so, I would be happy to implement this if someone can point be in the right direction.

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 locating rustfmt’s enum-formatting logic and related formatting tests; the issue provides the enum example and desired output. Check whether the behavior belongs in a rustfmt.toml option or should be the default, then run the formatter tests. Done means coverage showing that a comment on one variant does not expand unrelated variants, while a documented field still expands its own variant.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.