Skip to content

Commit 203e6d2

Browse files
authored
Merge pull request rust-lang#3294 from rchaser53/issue-3278
change new line point in the case of no args
2 parents 35d5ef7 + f92f3e3 commit 203e6d2

File tree

9 files changed

+107
-5
lines changed

9 files changed

+107
-5
lines changed

src/items.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use comment::{
2424
combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
2525
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented,
2626
};
27-
use config::{BraceStyle, Config, Density, IndentStyle};
27+
use config::{BraceStyle, Config, Density, IndentStyle, Version};
2828
use expr::{
2929
format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
3030
ExprType, RhsTactics,
@@ -2064,6 +2064,8 @@ fn rewrite_fn_base(
20642064
} && !fd.inputs.is_empty();
20652065

20662066
let mut args_last_line_contains_comment = false;
2067+
let mut no_args_and_over_max_width = false;
2068+
20672069
if put_args_in_block {
20682070
arg_indent = indent.block_indent(context.config);
20692071
result.push_str(&arg_indent.to_string_with_newline(context.config));
@@ -2083,10 +2085,19 @@ fn rewrite_fn_base(
20832085
.lines()
20842086
.last()
20852087
.map_or(false, |last_line| last_line.contains("//"));
2086-
if closing_paren_overflow_max_width || args_last_line_contains_comment {
2087-
result.push_str(&indent.to_string_with_newline(context.config));
2088+
2089+
if context.config.version() == Version::Two {
2090+
result.push(')');
2091+
if closing_paren_overflow_max_width || args_last_line_contains_comment {
2092+
result.push_str(&indent.to_string_with_newline(context.config));
2093+
no_args_and_over_max_width = true;
2094+
}
2095+
} else {
2096+
if closing_paren_overflow_max_width || args_last_line_contains_comment {
2097+
result.push_str(&indent.to_string_with_newline(context.config));
2098+
}
2099+
result.push(')');
20882100
}
2089-
result.push(')');
20902101
}
20912102

20922103
// Return type.
@@ -2126,7 +2137,14 @@ fn rewrite_fn_base(
21262137
result.push_str(&indent.to_string_with_newline(context.config));
21272138
indent
21282139
} else {
2129-
result.push(' ');
2140+
if context.config.version() == Version::Two {
2141+
if arg_str.len() != 0 || !no_args_and_over_max_width {
2142+
result.push(' ');
2143+
}
2144+
} else {
2145+
result.push(' ');
2146+
}
2147+
21302148
Indent::new(indent.block_indent, last_line_width(&result))
21312149
};
21322150

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-version: One
2+
3+
pub fn parse_conditional<'a, I: 'a>(
4+
) -> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
5+
where
6+
I: Stream<Item = char>,
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-version: Two
2+
3+
pub fn parse_conditional<'a, I: 'a>()
4+
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
5+
where
6+
I: Stream<Item = char>,
7+
{
8+
}

tests/source/long-fn-1.rs renamed to tests/source/long-fn-1/version_one.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-version: One
12
// Tests that a function which is almost short enough, but not quite, gets
23
// formatted correctly.
34

tests/source/long-fn-1/version_two.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// rustfmt-version: Two
2+
// Tests that a function which is almost short enough, but not quite, gets
3+
// formatted correctly.
4+
5+
impl Foo {
6+
fn some_input(&mut self, input: Input, input_path: Option<PathBuf>, ) -> (Input, Option<PathBuf>) {}
7+
8+
fn some_inpu(&mut self, input: Input, input_path: Option<PathBuf>) -> (Input, Option<PathBuf>) {}
9+
}
10+
11+
// #1843
12+
#[allow(non_snake_case)]
13+
pub extern "C" fn Java_com_exonum_binding_storage_indices_ValueSetIndexProxy_nativeContainsByHash() -> bool {
14+
false
15+
}
16+
17+
// #3009
18+
impl Something {
19+
fn my_function_name_is_way_to_long_but_used_as_a_case_study_or_an_example_its_fine(
20+
) -> Result< (), String > {}
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-version: One
2+
3+
pub fn parse_conditional<'a, I: 'a>(
4+
) -> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
5+
where
6+
I: Stream<Item = char>,
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-version: Two
2+
3+
pub fn parse_conditional<'a, I: 'a>()
4+
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
5+
where
6+
I: Stream<Item = char>,
7+
{
8+
}

tests/target/long-fn-1.rs renamed to tests/target/long-fn-1/version_one.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// rustfmt-version: One
12
// Tests that a function which is almost short enough, but not quite, gets
23
// formatted correctly.
34

tests/target/long-fn-1/version_two.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-version: Two
2+
// Tests that a function which is almost short enough, but not quite, gets
3+
// formatted correctly.
4+
5+
impl Foo {
6+
fn some_input(
7+
&mut self,
8+
input: Input,
9+
input_path: Option<PathBuf>,
10+
) -> (Input, Option<PathBuf>) {
11+
}
12+
13+
fn some_inpu(&mut self, input: Input, input_path: Option<PathBuf>) -> (Input, Option<PathBuf>) {
14+
}
15+
}
16+
17+
// #1843
18+
#[allow(non_snake_case)]
19+
pub extern "C" fn Java_com_exonum_binding_storage_indices_ValueSetIndexProxy_nativeContainsByHash()
20+
-> bool {
21+
false
22+
}
23+
24+
// #3009
25+
impl Something {
26+
fn my_function_name_is_way_to_long_but_used_as_a_case_study_or_an_example_its_fine()
27+
-> Result<(), String> {
28+
}
29+
}

0 commit comments

Comments
 (0)