Skip to content

PreferSameLine brace_style with where_single_line = true causes inconsistent braces for function declarations #6585

Open
@lukewilson2002

Description

@lukewilson2002

Function declarations that had their parameters "multi-lined" were forcing the brace after the where clause to the next line like so:

fn fn_with_everything_long_and_body<T, U, 'a>(
    aaaa: f64,
    bbbb: f64,
    cccc: f64,
    dddd: f64,
    eeee: f64,
    ffff: f64,       // Note: multiple arguments here
) -> Result<
    HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>,
    Box<dyn Error + Send + Sync + 'static>,
>
where
    T: Debug + Display + Clone + Send + Sync + 'static,
    U: Iterator<Item = &'a T> + ExactSizeIterator,
{     // <- brace here, incorrect ?
}

Whereas for an equivalent function that had no multi-lined arguments the brace would be on the same line as the last where predicate:

fn fn_with_long_return_and_where_and_body<T, U, 'a>(
    a: f64,       // Note: single argument here
) -> Result<
    HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>,
    Box<dyn Error + Send + Sync + 'static>,
>
where
    T: Debug + Display + Clone + Send + Sync + 'static,
    U: Iterator<Item = &'a T> + ExactSizeIterator, {            // <- brace here, correct by PreferSameLine
}

I've tracked it down to these lines of code:

rustfmt/src/items.rs

Lines 2750 to 2751 in 0332da0

force_new_line_for_brace |=
is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();


With PreferSameLine, should the opening brace always be on the same line as the last where predicate? If so, then a condition can be added to not force the brace to a new line if brace_style is PreferSameLine:

- force_new_line_for_brace |=
-     is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
+ force_new_line_for_brace |= is_params_multi_lined
+    && context.config.where_single_line()
+    && !where_clause_str.is_empty()
+    && context.config.brace_style() != BraceStyle::PreferSameLine;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions