Skip to content

Commit

Permalink
Correctly indent function return types on new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed Aug 10, 2020
1 parent f817383 commit 38aa435
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error_on_line_overflow = true
error_on_unformatted = true
# error_on_line_overflow = true
# error_on_unformatted = true
17 changes: 6 additions & 11 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,7 @@ fn rewrite_fn_base(

let mut params_last_line_contains_comment = false;
let mut no_params_and_over_max_width = false;
let mut ret_on_nl = false;

if put_params_in_block {
param_indent = indent.block_indent(context.config);
Expand All @@ -2355,7 +2356,7 @@ fn rewrite_fn_base(

if closing_paren_overflow_max_width {
result.push(')');
result.push_str(&indent.to_string_with_newline(context.config));
ret_on_nl = true;
no_params_and_over_max_width = true;
} else if params_last_line_contains_comment {
result.push_str(&indent.to_string_with_newline(context.config));
Expand All @@ -2368,7 +2369,6 @@ 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 @@ -2389,7 +2389,7 @@ fn rewrite_fn_base(
ret_on_nl = sig_length > context.config.max_width();
ret_on_nl
}
};
} || ret_on_nl;
let ret_shape = if ret_should_indent {
if context.config.indent_style() == IndentStyle::Visual {
let indent = if param_str.is_empty() {
Expand All @@ -2407,17 +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() || ret_on_nl {
if 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_on_nl {
ret_shape.offset_left(4).unwrap_or(ret_shape)
} else {
ret_shape.indent = ret_shape.indent + 4;
ret_shape
};
ret_shape = ret_shape.block_indent(4);
}

result.push_str(&ret_shape.indent.to_string_with_newline(context.config));
Expand All @@ -2434,7 +2429,7 @@ fn rewrite_fn_base(
.unwrap_or(ret_shape)
};

if multi_line_ret_str || ret_should_indent {
if ret_should_indent {
// Now that we know the proper indent and width, we need to
// re-layout the return type.
let ret_str = fd.output.rewrite(context, ret_shape)?;
Expand Down
8 changes: 5 additions & 3 deletions tests/target/issue-2672.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ mod asdf {

impl Something {
fn my_function_name_is_way_to_long_but_used_as_a_case_study_or_an_example_its_fine()
-> Result<(), String> {
-> Result<(), String>
{
}
}

impl Something {
fn my_function_name()
-> HashMap<(String, String, (String, String)), (String, String, String, String)> {
-> HashMap<(String, String, (String, String)), (String, String, String, String)>
{
}
}

Expand All @@ -46,7 +48,7 @@ mod A {
mod L {
mod M {
fn setup_happy_path()
-> Result<String, CustomTypeA>
-> Result<String, CustomTypeA>
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/target/issue-3278.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn parse_conditional<'a, I: 'a>()
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
where
I: Stream<Item = char>,
{
Expand Down
6 changes: 4 additions & 2 deletions tests/target/long-fn-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ impl Foo {
// #1843
#[allow(non_snake_case)]
pub extern "C" fn Java_com_exonum_binding_storage_indices_ValueSetIndexProxy_nativeContainsByHash()
-> bool {
-> bool
{
false
}

// #3009
impl Something {
fn my_function_name_is_way_to_long_but_used_as_a_case_study_or_an_example_its_fine()
-> Result<(), String> {
-> Result<(), String>
{
}
}

0 comments on commit 38aa435

Please sign in to comment.