diff --git a/src/items.rs b/src/items.rs index 0e814644304..65b990be18b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2500,6 +2500,7 @@ fn rewrite_fn_base( ret_str_len, fn_brace_style, multi_line_ret_str, + where_clause, ); debug!( @@ -2602,10 +2603,20 @@ fn rewrite_fn_base( // the closing parenthesis of the param and the arrow '->' is considered. let mut sig_length = result.len() + indent.width() + ret_str_len + 1; - // If there is no where-clause, take into account the space after the return type - // and the brace. + // If there is no where-clause. if where_clause.predicates.is_empty() { - sig_length += 2; + if context.config.style_edition() >= StyleEdition::Edition2027 { + let line_ending_overhead = match fn_brace_style { + FnBraceStyle::NextLine => 0, // No brace to account for + FnBraceStyle::SameLine => 2, // Trailing space and brace, e.g. ` {` + FnBraceStyle::None => 1, // Trailing `;` + }; + sig_length += line_ending_overhead; + } else { + // Take into account the space after the return type and the brace. + // 2 = ' {' + sig_length += 2; + } } sig_length > context.config.max_width() @@ -2889,6 +2900,7 @@ fn compute_budgets_for_params( ret_str_len: usize, fn_brace_style: FnBraceStyle, force_vertical_layout: bool, + where_clause: &ast::WhereClause, ) -> (usize, usize, Indent) { debug!( "compute_budgets_for_params {} {:?}, {}, {:?}", @@ -2903,7 +2915,15 @@ fn compute_budgets_for_params( let overhead = if ret_str_len == 0 { 2 } else { 3 }; let mut used_space = indent.width() + result.len() + ret_str_len + overhead; match fn_brace_style { - FnBraceStyle::None => used_space += 1, // 1 = `;` + _ if context.config.style_edition() >= StyleEdition::Edition2027 + && where_clause.predicates.len() > 0 => + { + // Don't add anything to `used_space` if we have a where clause. + // For all `FnBraceStyle` values if we have a where cluase that can't fit + // on the current line it'll be written to the next line. + // Therefore, we don't need to account for a trailing `;` or `{}` + } + FnBraceStyle::None => used_space += 1, // 1 = `;` FnBraceStyle::SameLine => used_space += 2, // 2 = `{}` FnBraceStyle::NextLine => (), } diff --git a/tests/source/issue-6539/brace_next_line.rs b/tests/source/issue-6539/brace_next_line.rs new file mode 100644 index 00000000000..04dbc84f4e4 --- /dev/null +++ b/tests/source/issue-6539/brace_next_line.rs @@ -0,0 +1,16 @@ +// rustfmt-style_edition: 2027 +// rustfmt-brace_style: AlwaysNextLine +// rustfmt-where_single_line: false + +pub trait Trait +{ + fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause(&self, a: T, bb: f64) -> f64 where T: Debug; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause2(&self, aaa: T, bbb: f64) -> f64 where T: Debug; + + fn an_over_one_hundred_column_fn_decl_with_body(&self, aaaaa: f64, bbbbb: f64, ccc: f64) -> f64 {} + + fn an_over_one_hundred_column_fn_decl_with_body_and_where_clause(&self, aaaaa: f64) -> f64 where T: Debug {} +} diff --git a/tests/source/issue-6539/brace_next_line_where_single_line.rs b/tests/source/issue-6539/brace_next_line_where_single_line.rs new file mode 100644 index 00000000000..feb75359939 --- /dev/null +++ b/tests/source/issue-6539/brace_next_line_where_single_line.rs @@ -0,0 +1,16 @@ +// rustfmt-style_edition: 2027 +// rustfmt-brace_style: AlwaysNextLine +// rustfmt-where_single_line: true + +pub trait Trait +{ + fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause(&self, a: T, bb: f64) -> f64 where T: Debug; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause2(&self, aaa: T, bbb: f64) -> f64 where T: Debug; + + fn an_over_one_hundred_column_fn_decl_with_body(&self, aaaaa: f64, bbbbb: f64, ccc: f64) -> f64 {} + + fn an_over_one_hundred_column_fn_decl_with_body_and_where_clause(&self, aaaaa: f64) -> f64 where T: Debug {} +} diff --git a/tests/source/issue-6539/default.rs b/tests/source/issue-6539/default.rs new file mode 100644 index 00000000000..568ced6a2fd --- /dev/null +++ b/tests/source/issue-6539/default.rs @@ -0,0 +1,15 @@ +// rustfmt-style_edition: 2027 + +pub trait Trait { + fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64; + + fn a_one_hundred_one_column_fn_decl_no_body(self, a: f64, b: f64, c: f64, d: f64, e: f64) -> f64; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause(&self, a: T, bb: f64) -> f64 where T: Debug; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause2(&self, aaa: T, bbb: f64) -> f64 where T: Debug; + + fn an_over_one_hundred_column_fn_decl_with_body(&self, aaaaa: f64, bbbbb: f64, ccc: f64) -> f64 {} + + fn an_over_one_hundred_column_fn_decl_with_body_and_where_clause(&self, aaaaa: f64) -> f64 where T: Debug {} +} diff --git a/tests/target/issue-6539/brace_next_line.rs b/tests/target/issue-6539/brace_next_line.rs new file mode 100644 index 00000000000..35cb57fa6ca --- /dev/null +++ b/tests/target/issue-6539/brace_next_line.rs @@ -0,0 +1,30 @@ +// rustfmt-style_edition: 2027 +// rustfmt-brace_style: AlwaysNextLine +// rustfmt-where_single_line: false + +pub trait Trait +{ + fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause(&self, a: T, bb: f64) -> f64 + where + T: Debug; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause2( + &self, + aaa: T, + bbb: f64, + ) -> f64 + where + T: Debug; + + fn an_over_one_hundred_column_fn_decl_with_body(&self, aaaaa: f64, bbbbb: f64, ccc: f64) -> f64 + { + } + + fn an_over_one_hundred_column_fn_decl_with_body_and_where_clause(&self, aaaaa: f64) -> f64 + where + T: Debug, + { + } +} diff --git a/tests/target/issue-6539/brace_next_line_where_single_line.rs b/tests/target/issue-6539/brace_next_line_where_single_line.rs new file mode 100644 index 00000000000..bdf7cdd9c02 --- /dev/null +++ b/tests/target/issue-6539/brace_next_line_where_single_line.rs @@ -0,0 +1,28 @@ +// rustfmt-style_edition: 2027 +// rustfmt-brace_style: AlwaysNextLine +// rustfmt-where_single_line: true + +pub trait Trait +{ + fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause(&self, a: T, bb: f64) -> f64 + where T: Debug; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause2( + &self, + aaa: T, + bbb: f64, + ) -> f64 + where + T: Debug; + + fn an_over_one_hundred_column_fn_decl_with_body(&self, aaaaa: f64, bbbbb: f64, ccc: f64) -> f64 + { + } + + fn an_over_one_hundred_column_fn_decl_with_body_and_where_clause(&self, aaaaa: f64) -> f64 + where T: Debug + { + } +} diff --git a/tests/target/issue-6539/default.rs b/tests/target/issue-6539/default.rs new file mode 100644 index 00000000000..2392a52cf08 --- /dev/null +++ b/tests/target/issue-6539/default.rs @@ -0,0 +1,40 @@ +// rustfmt-style_edition: 2027 + +pub trait Trait { + fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64; + + fn a_one_hundred_one_column_fn_decl_no_body( + self, + a: f64, + b: f64, + c: f64, + d: f64, + e: f64, + ) -> f64; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause(&self, a: T, bb: f64) -> f64 + where + T: Debug; + + fn an_over_one_hundred_column_fn_decl_no_body_and_where_clause2( + &self, + aaa: T, + bbb: f64, + ) -> f64 + where + T: Debug; + + fn an_over_one_hundred_column_fn_decl_with_body( + &self, + aaaaa: f64, + bbbbb: f64, + ccc: f64, + ) -> f64 { + } + + fn an_over_one_hundred_column_fn_decl_with_body_and_where_clause(&self, aaaaa: f64) -> f64 + where + T: Debug, + { + } +}