Skip to content

Commit f817383

Browse files
authored
Indent return types separated from params by newline (#4368)
* 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
1 parent e91edf5 commit f817383

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Diff for: src/formatting/items.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,7 @@ fn rewrite_fn_base(
23682368

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

2388-
sig_length > context.config.max_width()
2389+
ret_on_nl = sig_length > context.config.max_width();
2390+
ret_on_nl
23892391
}
23902392
};
23912393
let ret_shape = if ret_should_indent {
@@ -2405,10 +2407,12 @@ fn rewrite_fn_base(
24052407
Shape::indented(indent, context.config)
24062408
} else {
24072409
let mut ret_shape = Shape::indented(indent, context.config);
2408-
if param_str.is_empty() {
2409-
// Aligning with non-existent params looks silly.
2410+
if param_str.is_empty() || ret_on_nl {
2411+
// Aligning with non-existent params looks silly, as does aligning the return
2412+
// type when it is on a newline entirely disconnected from the parentheses of
2413+
// the parameters.
24102414
force_new_line_for_brace = true;
2411-
ret_shape = if context.use_block_indent() {
2415+
ret_shape = if context.use_block_indent() && !ret_on_nl {
24122416
ret_shape.offset_left(4).unwrap_or(ret_shape)
24132417
} else {
24142418
ret_shape.indent = ret_shape.indent + 4;

Diff for: tests/target/configs/space_before_fn_sig_paren/max_width.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

1010
impl Story for () {

Diff for: tests/target/issue-4306.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-max_width: 80
2+
3+
trait GetMetadata {
4+
fn metadata(loc: ApiEndpointParameterLocation)
5+
-> Vec<ApiEndpointParameter>;
6+
}

0 commit comments

Comments
 (0)