Skip to content

Commit

Permalink
Indent return types separated from params by newline (#4368)
Browse files Browse the repository at this point in the history
* Indent return types separated from params by newline

 #3891 removed Version One formatting that did this correctly (https://github.com/rust-lang/rustfmt/pull/3891/files#diff-5db152a52bdaeae9dacd35f43a4a78ddL2342-L2344),
but at seemingly at the time there were no tests to catch the
regression.

This commit fix-forwards the formatting regression, though via a different
implementation because the original implementation would muddle the branch
for visual indentation and this fix, which is probably not preferrable
giving the existing complexity of the rewrite_required_fn method.

Closes #4366

* fixup! Indent return types separated from params by newline
  • Loading branch information
ayazhafiz authored Aug 7, 2020
1 parent e91edf5 commit f817383
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,7 @@ fn rewrite_fn_base(

// Return type.
if let ast::FnRetTy::Ty(..) = fd.output {
let mut ret_on_nl = false;
let ret_should_indent = match context.config.indent_style() {
// If our params are block layout then we surely must have space.
IndentStyle::Block if put_params_in_block || fd.inputs.is_empty() => false,
Expand All @@ -2385,7 +2386,8 @@ fn rewrite_fn_base(
sig_length += 2;
}

sig_length > context.config.max_width()
ret_on_nl = sig_length > context.config.max_width();
ret_on_nl
}
};
let ret_shape = if ret_should_indent {
Expand All @@ -2405,10 +2407,12 @@ fn rewrite_fn_base(
Shape::indented(indent, context.config)
} else {
let mut ret_shape = Shape::indented(indent, context.config);
if param_str.is_empty() {
// Aligning with non-existent params looks silly.
if param_str.is_empty() || ret_on_nl {
// Aligning with non-existent params looks silly, as does aligning the return
// type when it is on a newline entirely disconnected from the parentheses of
// the parameters.
force_new_line_for_brace = true;
ret_shape = if context.use_block_indent() {
ret_shape = if context.use_block_indent() && !ret_on_nl {
ret_shape.offset_left(4).unwrap_or(ret_shape)
} else {
ret_shape.indent = ret_shape.indent + 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

trait Story {
fn swap_context<T: 'static + Context + Send + Sync> (&mut self, context: T)
-> Option<Box<Context + Send + Sync>>;
-> Option<Box<Context + Send + Sync>>;
}

impl Story for () {
Expand Down
6 changes: 6 additions & 0 deletions tests/target/issue-4306.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-max_width: 80

trait GetMetadata {
fn metadata(loc: ApiEndpointParameterLocation)
-> Vec<ApiEndpointParameter>;
}

0 comments on commit f817383

Please sign in to comment.