rust-lang / rust-lang/rustfmt

control_brace_style="AlwaysSameLine" is not respected when "if" and "if let" line length is near max_width

Open
#5,349 2 comments 2 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

If the if let of if line length is near max_width, then the control_brace_style option of rustfmt pushes the opening brace to the next line. (Notice that this option is still unstable.)

I observe this behaviour for "rustfmt 1.4.38-nightly (9257f5aa 2022-05-21)".

In the examples given below, the closing parenthesis character in the non-wrapped if let- an if- expressions is the 100th character in the line.

"AlwaysSameLine"

unstable_features = true
control_brace_style = "AlwaysSameLine"

The formatting does match the expectations neither for if let, nor for if:

fn main() {
    if let Some(x) = some_function_with_a_long_list_of_args(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
    {
        return;
    }

    if some_boolean_function_with_a_long_name_and_a_long_list_of_args(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    {
        return;
    }
}

"AlwaysSameLine" + max_width=102

unstable_features = true
control_brace_style = "AlwaysSameLine"
max_width = 102

The formatting matches the expectations for both if let and if:

fn main() {
    if let Some(x) = some_function_with_a_long_list_of_args(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) {
        return;
    }

    if some_boolean_function_with_a_long_name_and_a_long_list_of_args(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) {
        return;
    }
}

"AlwaysSameLine" + max_width=99

unstable_features = true
control_brace_style = "AlwaysSameLine"
max_width = 99

The formatting does match the expectations for if let, but matches the expectations for if:

fn main() {
    if let Some(x) =
        some_function_with_a_long_list_of_args(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
    {
        return;
    }

    if some_boolean_function_with_a_long_name_and_a_long_list_of_args(
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
    ) {
        return;
    }
}

(A similar issue to the one shown in this example is mentioned here: https://github.com/rust-lang/rustfmt/issues/3560#issuecomment-493670516. In that one, the content of the if let is much less than max_width, but it gets folded like here, and the opening brace is pushed to the next line as well.)

"AlwaysNextLine"

unstable_features = true
control_brace_style = "AlwaysNextLine"
max_width = 102

The formatting matches the expectations for both if let and if:

fn main() {
    if let Some(x) = some_function_with_a_long_list_of_args(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
    {
        return;
    }

    if some_boolean_function_with_a_long_name_and_a_long_list_of_args(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    {
        return;
    }
}

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 issue with rustfmt using the provided control_brace_style, max_width, and if/if let examples. Then trace the formatter's handling of control braces and add coverage for the shown width boundaries. Done means AlwaysSameLine keeps the opening brace on the same line in both cases while AlwaysNextLine retains its documented behavior.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.