Skip to content

fix: wrap function without a body exceeding 100 characters (#6539) #6579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,7 @@ fn rewrite_fn_base(
ret_str_len,
fn_brace_style,
multi_line_ret_str,
where_clause,
);

debug!(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -2749,6 +2760,7 @@ fn rewrite_fn_base(
force_new_line_for_brace |= ends_with_comment;
force_new_line_for_brace |=
is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();

Ok((result, ends_with_comment, force_new_line_for_brace))
}

Expand Down Expand Up @@ -2889,6 +2901,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 {} {:?}, {}, {:?}",
Expand All @@ -2903,7 +2916,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 => (),
}
Expand Down
73 changes: 73 additions & 0 deletions tests/source/issue-6539/brace_next_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// rustfmt-style_edition: 2027
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-where_single_line: false

// Top-level functions

// Short function
fn short_fn(a: f64, b: f64) -> f64;

// Function with wrapping return type and no where clause
fn fn_with_long_return_type(a: f64) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>>;

// Function with non-wrapping return type and where clause
fn fn_with_short_where<T>(a: f64, b: T) -> f64 where T: Debug;

// Function that wraps at a simple return type
fn fn_with_wrapping_return_type<T>(aaaaaa: f64, bbbbbb: T, cccccc: f64, dddddd: f64, eeee: f64) -> f64;

// Function that wraps at the where clause
fn fn_with_wrapping_where_clause<T>(aaaaaa: f64, bbbbbb: T, cccccc: f64, dddddd: f64) -> f64 where T: Debug;

// Function with both wrapping return type and wrapping where clause
fn fn_with_long_return_and_where<T, U, 'a>(a: f64) -> 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;

// Function with wrapping arguments, return type, and where clause
fn fn_with_everything_long<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64, ffff: f64) -> 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;

// Same variations with bodies
fn short_fn_with_body(a: f64, b: f64) -> f64 {}

fn fn_with_long_return_type_and_body(a: f64) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>> {}

fn fn_with_short_where_and_body<T>(a: f64, b: T) -> f64 where T: Debug {}

fn fn_with_wrapping_return_type_and_body<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64, ee: f64) -> f64 {}

fn fn_with_wrapping_where_clause_and_body<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64) -> f64 where T: Debug {}

fn fn_with_long_return_and_where_and_body<T, U, 'a>(a: f64) -> 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 {}

fn fn_with_everything_long_and_body<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64, ffff: f64) -> 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 {}

// Trait methods
pub trait Trait {
fn short_method(a: f64, b: f64) -> f64;

fn method_with_long_return(&self) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>>;

fn method_with_short_where<T>(&self, a: f64, b: T) -> f64 where T: Debug;

fn method_with_wrapping_return_type<T>(self, aaa: f64, bbb: T, ccc: f64, ddd: f64, ee: f64) -> f64;

fn method_with_wrapping_where_clause<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64) -> f64 where T: Debug;

fn method_with_long_return_and_where<T, U, 'a>(self) -> 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;

fn method_with_everything_long<T, U, 'a>(&self, aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64) -> 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;

// Same variations with bodies
fn short_method_with_body(a: f64, b: f64) -> f64 {}

fn method_with_long_return_and_body(&self) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>> {}

fn method_with_short_where_and_body<T>(&self, a: f64, b: T) -> f64 where T: Debug {}

fn method_with_wrapping_return_type_and_body<T>(aaaa: f64, bb: T, cc: f64, d: f64, e: f64) -> f64 {}

fn method_with_wrapping_where_clause_and_body<T>(aaa: f64, bb: T, cc: f64, d: f64) -> f64 where T: Debug {}

fn method_with_long_return_and_where_and_body<T, U, 'a>(self) -> 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 {}

fn method_with_everything_long_and_body<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64) -> 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 {}
}
73 changes: 73 additions & 0 deletions tests/source/issue-6539/brace_next_line_where_single_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// rustfmt-style_edition: 2027
// rustfmt-brace_style: AlwaysNextLine
// rustfmt-where_single_line: true

// Top-level functions

// Short function
fn short_fn(a: f64, b: f64) -> f64;

// Function with wrapping return type and no where clause
fn fn_with_long_return_type(a: f64) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>>;

// Function with non-wrapping return type and where clause
fn fn_with_short_where<T>(a: f64, b: T) -> f64 where T: Debug;

// Function that wraps at a simple return type
fn fn_with_wrapping_return_type<T>(aaaaaa: f64, bbbbbb: T, cccccc: f64, dddddd: f64, eeee: f64) -> f64;

// Function that wraps at the where clause
fn fn_with_wrapping_where_clause<T>(aaaaaa: f64, bbbbbb: T, cccccc: f64, dddddd: f64) -> f64 where T: Debug;

// Function with both wrapping return type and wrapping where clause
fn fn_with_long_return_and_where<T, U, 'a>(a: f64) -> 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;

// Function with wrapping arguments, return type, and where clause
fn fn_with_everything_long<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64, ffff: f64) -> 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;

// Same variations with bodies
fn short_fn_with_body(a: f64, b: f64) -> f64 {}

fn fn_with_long_return_type_and_body(a: f64) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>> {}

fn fn_with_short_where_and_body<T>(a: f64, b: T) -> f64 where T: Debug {}

fn fn_with_wrapping_return_type_and_body<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64, ee: f64) -> f64 {}

fn fn_with_wrapping_where_clause_and_body<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64) -> f64 where T: Debug {}

fn fn_with_long_return_and_where_and_body<T, U, 'a>(a: f64) -> 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 {}

fn fn_with_everything_long_and_body<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64, ffff: f64) -> 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 {}

// Trait methods
pub trait Trait {
fn short_method(a: f64, b: f64) -> f64;

fn method_with_long_return(&self) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>>;

fn method_with_short_where<T>(&self, a: f64, b: T) -> f64 where T: Debug;

fn method_with_wrapping_return_type<T>(self, aaa: f64, bbb: T, ccc: f64, ddd: f64, ee: f64) -> f64;

fn method_with_wrapping_where_clause<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64) -> f64 where T: Debug;

fn method_with_long_return_and_where<T, U, 'a>(self) -> 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;

fn method_with_everything_long<T, U, 'a>(&self, aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64) -> 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;

// Same variations with bodies
fn short_method_with_body(a: f64, b: f64) -> f64 {}

fn method_with_long_return_and_body(&self) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>> {}

fn method_with_short_where_and_body<T>(&self, a: f64, b: T) -> f64 where T: Debug {}

fn method_with_wrapping_return_type_and_body<T>(aaaa: f64, bb: T, cc: f64, d: f64, e: f64) -> f64 {}

fn method_with_wrapping_where_clause_and_body<T>(aaa: f64, bb: T, cc: f64, d: f64) -> f64 where T: Debug {}

fn method_with_long_return_and_where_and_body<T, U, 'a>(self) -> 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 {}

fn method_with_everything_long_and_body<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64) -> 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 {}
}
73 changes: 73 additions & 0 deletions tests/source/issue-6539/prefer_same_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// rustfmt-style_edition: 2027
// rustfmt-brace_style: PreferSameLine
// rustfmt-where_single_line: false

// Top-level functions

// Short function
fn short_fn(a: f64, b: f64) -> f64;

// Function with wrapping return type and no where clause
fn fn_with_long_return_type(a: f64) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>>;

// Function with non-wrapping return type and where clause
fn fn_with_short_where<T>(a: f64, b: T) -> f64 where T: Debug;

// Function that wraps at a simple return type
fn fn_with_wrapping_return_type<T>(aaaaaa: f64, bbbbbb: T, cccccc: f64, dddddd: f64, eeee: f64) -> f64;

// Function that wraps at the where clause
fn fn_with_wrapping_where_clause<T>(aaaaaa: f64, bbbbbb: T, cccccc: f64, dddddd: f64) -> f64 where T: Debug;

// Function with both wrapping return type and wrapping where clause
fn fn_with_long_return_and_where<T, U, 'a>(a: f64) -> 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;

// Function with wrapping arguments, return type, and where clause
fn fn_with_everything_long<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64, ffff: f64) -> 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;

// Same variations with bodies
fn short_fn_with_body(a: f64, b: f64) -> f64 {}

fn fn_with_long_return_type_and_body(a: f64) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>> {}

fn fn_with_short_where_and_body<T>(a: f64, b: T) -> f64 where T: Debug {}

fn fn_with_wrapping_return_type_and_body<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64, ee: f64) -> f64 {}

fn fn_with_wrapping_where_clause_and_body<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64) -> f64 where T: Debug {}

fn fn_with_long_return_and_where_and_body<T, U, 'a>(a: f64) -> 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 {}

fn fn_with_everything_long_and_body<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64, ffff: f64) -> 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 {}

// Trait methods
pub trait Trait {
fn short_method(a: f64, b: f64) -> f64;

fn method_with_long_return(&self) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>>;

fn method_with_short_where<T>(&self, a: f64, b: T) -> f64 where T: Debug;

fn method_with_wrapping_return_type<T>(self, aaa: f64, bbb: T, ccc: f64, ddd: f64, ee: f64) -> f64;

fn method_with_wrapping_where_clause<T>(aaaaaa: f64, bbbbbb: T, ccc: f64, ddd: f64) -> f64 where T: Debug;

fn method_with_long_return_and_where<T, U, 'a>(self) -> 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;

fn method_with_everything_long<T, U, 'a>(&self, aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64) -> 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;

// Same variations with bodies
fn short_method_with_body(a: f64, b: f64) -> f64 {}

fn method_with_long_return_and_body(&self) -> Result<HashMap<String, Vec<(SomeLongTypeName, AnotherLongTypeName, YetAnotherType)>>, Box<dyn Error + Send + Sync + 'static>> {}

fn method_with_short_where_and_body<T>(&self, a: f64, b: T) -> f64 where T: Debug {}

fn method_with_wrapping_return_type_and_body<T>(aaaa: f64, bb: T, cc: f64, d: f64, e: f64) -> f64 {}

fn method_with_wrapping_where_clause_and_body<T>(aaa: f64, bb: T, cc: f64, d: f64) -> f64 where T: Debug {}

fn method_with_long_return_and_where_and_body<T, U, 'a>(self) -> 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 {}

fn method_with_everything_long_and_body<T, U, 'a>(aaaa: f64, bbbb: f64, cccc: f64, dddd: f64, eeee: f64) -> 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 {}
}
Loading
Loading