rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.06k stars 892 forks source link

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

Open pozdneev opened 2 years ago

pozdneev commented 2 years ago

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;
    }
}
ytmimi commented 2 years ago

linking tracking issue for control_brace_style https://github.com/rust-lang/rustfmt/issues/3377

mqus commented 1 year ago

I just want to note that AlwaysSameLine is the default, so at least the first buggy example should be reproducible with only stable options (or no options at all) and the others only really configure max_width which probably does not have anything to do with this bug, so the label is probably wrong

This bug seems similar to #3560.