rust-lang / rust-lang/rustfmt

Wish to have some new options about line break style

Open
#6,350 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

version

rustfmt 1.7.1-nightly (94885bc6 2024-09-01)

rustfmt.toml
max_width = 80
use_small_heuristics = "Max"
example
fn _hincrbyex(
    _a: &mut i64,
    _key: &i64,
    _field: &i64,
    _delta: i64,
    _iexpsec: i64,
    _b0: bool,
) -> bool {
    false
}

fn test() {
    let mut conn: i64 = 0;
    let key: i64 = 0;
    let field: i64 = 0;
    let delta: i64 = 0;
    let iexpsec: i64 = 0;

    let is_xxxxxxxx: bool = false;
    let is_xxxxxxxxx: bool = false;
    let is_xxxxxxxxxxxxxxx: bool = false;
    let is_xxxxxxxxxxxxxxxx: bool = false;

    // single line: exactly 80 ------------------------------------------------|
    let _fut = _hincrbyex(&mut conn, &key, &field, delta, iexpsec, is_xxxxxxxx);

    // style 1: 81 ------------------------------------------------------------|
    let _fut =
        _hincrbyex(&mut conn, &key, &field, delta, iexpsec, is_xxxxxxxxx);

    // style 1: exactly 80 for the second line --------------------------------|
    let _fut =
        _hincrbyex(&mut conn, &key, &field, delta, iexpsec, is_xxxxxxxxxxxxxxx);

    // style 2: 81 for the second line in style 1 -----------------------------|
    let _fut = _hincrbyex(
        &mut conn,
        &key,
        &field,
        delta,
        iexpsec,
        is_xxxxxxxxxxxxxxxx,
    );
}

According to the above example, rustfmt have two line break styles for long statement. But I really don't like the style 1 because it looks strange in some cases. I have to make some tricky adjustments such as rename the function name / rename some argument names / ... to change the style 1 to single line or style 2. And it bothers me too in some other situations such as match-arms.
I wish to have some new options in rustfmt.toml to disable the style 1, a statement should directly be formatted to the style 2 from single line if it's too long. Thanks~

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 two line-break styles from the issue's rustfmt.toml configuration and Rust example. Read the formatter logic for long statements and match arms, then determine where an option would control the choice. Done means the option is documented and the examples format directly from a single line to the fully broken style, with tests covering both enabled and default behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.