Skip to content

Commit

Permalink
Merge pull request rust-lang#3294 from rchaser53/issue-3278
Browse files Browse the repository at this point in the history
change new line point in the case of no args
  • Loading branch information
topecongiro authored Jan 27, 2019
2 parents 35d5ef7 + f92f3e3 commit 203e6d2
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use comment::{
combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented,
};
use config::{BraceStyle, Config, Density, IndentStyle};
use config::{BraceStyle, Config, Density, IndentStyle, Version};
use expr::{
format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
ExprType, RhsTactics,
Expand Down Expand Up @@ -2064,6 +2064,8 @@ fn rewrite_fn_base(
} && !fd.inputs.is_empty();

let mut args_last_line_contains_comment = false;
let mut no_args_and_over_max_width = false;

if put_args_in_block {
arg_indent = indent.block_indent(context.config);
result.push_str(&arg_indent.to_string_with_newline(context.config));
Expand All @@ -2083,10 +2085,19 @@ fn rewrite_fn_base(
.lines()
.last()
.map_or(false, |last_line| last_line.contains("//"));
if closing_paren_overflow_max_width || args_last_line_contains_comment {
result.push_str(&indent.to_string_with_newline(context.config));

if context.config.version() == Version::Two {
result.push(')');
if closing_paren_overflow_max_width || args_last_line_contains_comment {
result.push_str(&indent.to_string_with_newline(context.config));
no_args_and_over_max_width = true;
}
} else {
if closing_paren_overflow_max_width || args_last_line_contains_comment {
result.push_str(&indent.to_string_with_newline(context.config));
}
result.push(')');
}
result.push(')');
}

// Return type.
Expand Down Expand Up @@ -2126,7 +2137,14 @@ fn rewrite_fn_base(
result.push_str(&indent.to_string_with_newline(context.config));
indent
} else {
result.push(' ');
if context.config.version() == Version::Two {
if arg_str.len() != 0 || !no_args_and_over_max_width {
result.push(' ');
}
} else {
result.push(' ');
}

Indent::new(indent.block_indent, last_line_width(&result))
};

Expand Down
8 changes: 8 additions & 0 deletions tests/source/issue-3278/version_one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-version: One

pub fn parse_conditional<'a, I: 'a>(
) -> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
where
I: Stream<Item = char>,
{
}
8 changes: 8 additions & 0 deletions tests/source/issue-3278/version_two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-version: Two

pub fn parse_conditional<'a, I: 'a>()
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
where
I: Stream<Item = char>,
{
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-version: One
// Tests that a function which is almost short enough, but not quite, gets
// formatted correctly.

Expand Down
21 changes: 21 additions & 0 deletions tests/source/long-fn-1/version_two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// rustfmt-version: Two
// Tests that a function which is almost short enough, but not quite, gets
// formatted correctly.

impl Foo {
fn some_input(&mut self, input: Input, input_path: Option<PathBuf>, ) -> (Input, Option<PathBuf>) {}

fn some_inpu(&mut self, input: Input, input_path: Option<PathBuf>) -> (Input, Option<PathBuf>) {}
}

// #1843
#[allow(non_snake_case)]
pub extern "C" fn Java_com_exonum_binding_storage_indices_ValueSetIndexProxy_nativeContainsByHash() -> 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 > {}
}
8 changes: 8 additions & 0 deletions tests/target/issue-3278/version_one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-version: One

pub fn parse_conditional<'a, I: 'a>(
) -> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
where
I: Stream<Item = char>,
{
}
8 changes: 8 additions & 0 deletions tests/target/issue-3278/version_two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-version: Two

pub fn parse_conditional<'a, I: 'a>()
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
where
I: Stream<Item = char>,
{
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// rustfmt-version: One
// Tests that a function which is almost short enough, but not quite, gets
// formatted correctly.

Expand Down
29 changes: 29 additions & 0 deletions tests/target/long-fn-1/version_two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// rustfmt-version: Two
// Tests that a function which is almost short enough, but not quite, gets
// formatted correctly.

impl Foo {
fn some_input(
&mut self,
input: Input,
input_path: Option<PathBuf>,
) -> (Input, Option<PathBuf>) {
}

fn some_inpu(&mut self, input: Input, input_path: Option<PathBuf>) -> (Input, Option<PathBuf>) {
}
}

// #1843
#[allow(non_snake_case)]
pub extern "C" fn Java_com_exonum_binding_storage_indices_ValueSetIndexProxy_nativeContainsByHash()
-> 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> {
}
}

0 comments on commit 203e6d2

Please sign in to comment.