Skip to content

Commit 19d6a3c

Browse files
committed
Put the opening brace of impl on the next line
We put the opening brace on the next line if the following conditions hold: 1. the result before '{' ends with comments or contains newline 2. the last line of the result before '{' is not extendable (i.e. consists of '>' and whitespaces).
1 parent c355f38 commit 19d6a3c

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

src/items.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use types::join_bounds;
3333
use utils::{colon_spaces, contains_skip, first_line_width, format_abi, format_constness,
3434
format_defaultness, format_mutability, format_unsafety, format_visibility,
3535
is_attributes_extendable, last_line_contains_single_line_comment,
36-
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, starts_with_newline,
37-
stmt_expr, trimmed_last_line_width};
36+
last_line_extendable, last_line_used_width, last_line_width, mk_sp,
37+
semicolon_for_expr, starts_with_newline, stmt_expr, trimmed_last_line_width};
3838
use vertical::rewrite_with_alignment;
3939
use visitor::FmtVisitor;
4040

@@ -639,8 +639,10 @@ pub fn format_impl(
639639
}
640640
result.push_str(&where_clause_str);
641641

642+
let need_newline = !last_line_extendable(&result)
643+
&& (last_line_contains_single_line_comment(&result) || result.contains('\n'));
642644
match context.config.brace_style() {
643-
_ if last_line_contains_single_line_comment(&result) => result.push_str(&sep),
645+
_ if need_newline => result.push_str(&sep),
644646
BraceStyle::AlwaysNextLine => result.push_str(&sep),
645647
BraceStyle::PreferSameLine => result.push(' '),
646648
BraceStyle::SameLineWhere => {

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub fn last_line_extendable(s: &str) -> bool {
178178
}
179179
for c in s.chars().rev() {
180180
match c {
181-
')' | ']' | '}' | '?' => continue,
181+
')' | ']' | '}' | '?' | '>' => continue,
182182
'\n' => break,
183183
_ if c.is_whitespace() => continue,
184184
_ => return false,

tests/target/trait.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,13 @@ trait Foo {
103103
// #2331
104104
trait MyTrait<
105105
AAAAAAAAAAAAAAAAAAAA,
106-
BBBBBBBBBBBBBBBBBBBB,
107-
CCCCCCCCCCCCCCCCCCCC,
108-
DDDDDDDDDDDDDDDDDDDD,
106+
BBBBBBBBBBBBBBBBBBBB,
107+
CCCCCCCCCCCCCCCCCCCC,
108+
DDDDDDDDDDDDDDDDDDDD,
109109
> {
110110
fn foo() {}
111111
}
112112

113-
114113
// Trait aliases
115114
trait FooBar = Foo + Bar;
116115
trait FooBar<A, B, C> = Foo + Bar;

0 commit comments

Comments
 (0)