Skip to content

Commit 7247e8a

Browse files
committed
Indent return types separated from params by newline
rust-lang#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 rust-lang#4366
1 parent e91edf5 commit 7247e8a

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/formatting/items.rs

+9-9
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,15 +2407,13 @@ 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() {
2412-
ret_shape.offset_left(4).unwrap_or(ret_shape)
2413-
} else {
2414-
ret_shape.indent = ret_shape.indent + 4;
2415-
ret_shape
2416-
};
2415+
2416+
ret_shape.indent = ret_shape.indent + 4;
24172417
}
24182418

24192419
result.push_str(&ret_shape.indent.to_string_with_newline(context.config));

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 () {

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)