Skip to content

Commit bf70bf9

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 a6bc5d6 commit bf70bf9

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/items.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ 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, trim_newlines, 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, trim_newlines,
38+
trimmed_last_line_width};
3839
use vertical::rewrite_with_alignment;
3940
use visitor::FmtVisitor;
4041

@@ -639,8 +640,10 @@ pub fn format_impl(
639640
}
640641
result.push_str(&where_clause_str);
641642

643+
let need_newline = !last_line_extendable(&result)
644+
&& (last_line_contains_single_line_comment(&result) || result.contains('\n'));
642645
match context.config.brace_style() {
643-
_ if last_line_contains_single_line_comment(&result) => result.push_str(&sep),
646+
_ if need_newline => result.push_str(&sep),
644647
BraceStyle::AlwaysNextLine => result.push_str(&sep),
645648
BraceStyle::PreferSameLine => result.push(' '),
646649
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)