Skip to content

Commit 4d50e7c

Browse files
mujpaocalebcartwright
authored andcommitted
Put empty trait braces on same line if possible
1 parent 9027db9 commit 4d50e7c

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

src/items.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,24 @@ pub(crate) fn format_trait(
11221122
}
11231123
}
11241124

1125+
let block_span = mk_sp(generics.where_clause.span.hi(), item.span.hi());
1126+
let snippet = context.snippet(block_span);
1127+
let open_pos = snippet.find_uncommented("{")? + 1;
1128+
11251129
match context.config.brace_style() {
11261130
_ if last_line_contains_single_line_comment(&result)
11271131
|| last_line_width(&result) + 2 > context.budget(offset.width()) =>
11281132
{
11291133
result.push_str(&offset.to_string_with_newline(context.config));
11301134
}
1135+
_ if context.config.empty_item_single_line()
1136+
&& trait_items.is_empty()
1137+
&& !result.contains('\n')
1138+
&& !contains_comment(&snippet[open_pos..]) =>
1139+
{
1140+
result.push_str(" {}");
1141+
return Some(result);
1142+
}
11311143
BraceStyle::AlwaysNextLine => {
11321144
result.push_str(&offset.to_string_with_newline(context.config));
11331145
}
@@ -1144,9 +1156,6 @@ pub(crate) fn format_trait(
11441156
}
11451157
result.push('{');
11461158

1147-
let block_span = mk_sp(generics.where_clause.span.hi(), item.span.hi());
1148-
let snippet = context.snippet(block_span);
1149-
let open_pos = snippet.find_uncommented("{")? + 1;
11501159
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
11511160

11521161
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// rustfmt-empty_item_single_line: false
3+
4+
fn function()
5+
{
6+
7+
}
8+
9+
struct Struct
10+
{
11+
12+
}
13+
14+
enum Enum
15+
{
16+
17+
}
18+
19+
trait Trait
20+
{
21+
22+
}
23+
24+
impl<T> Trait for T
25+
{
26+
27+
}
28+
29+
trait Trait2<T>
30+
where
31+
T: Copy + Display + Write + Read + FromStr, {}
32+
33+
trait Trait3<T>
34+
where
35+
T: Something
36+
+ SomethingElse
37+
+ Sync
38+
+ Send
39+
+ Display
40+
+ Debug
41+
+ Copy
42+
+ Hash
43+
+ Debug
44+
+ Display
45+
+ Write
46+
+ Read, {}

tests/source/item-brace-style-always-next-line.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,38 @@ mod M {
2727

2828
struct D<T> where T: Copy {}
2929
}
30+
31+
32+
fn function()
33+
{
34+
35+
}
36+
37+
trait Trait
38+
{
39+
40+
}
41+
42+
impl<T> Trait for T
43+
{
44+
45+
}
46+
47+
trait Trait2<T>
48+
where
49+
T: Copy + Display + Write + Read + FromStr, {}
50+
51+
trait Trait3<T>
52+
where
53+
T: Something
54+
+ SomethingElse
55+
+ Sync
56+
+ Send
57+
+ Display
58+
+ Debug
59+
+ Copy
60+
+ Hash
61+
+ Debug
62+
+ Display
63+
+ Write
64+
+ Read, {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// rustfmt-empty_item_single_line: false
3+
4+
fn function()
5+
{
6+
}
7+
8+
struct Struct {}
9+
10+
enum Enum {}
11+
12+
trait Trait
13+
{
14+
}
15+
16+
impl<T> Trait for T
17+
{
18+
}
19+
20+
trait Trait2<T>
21+
where
22+
T: Copy + Display + Write + Read + FromStr,
23+
{
24+
}
25+
26+
trait Trait3<T>
27+
where
28+
T: Something
29+
+ SomethingElse
30+
+ Sync
31+
+ Send
32+
+ Display
33+
+ Debug
34+
+ Copy
35+
+ Hash
36+
+ Debug
37+
+ Display
38+
+ Write
39+
+ Read,
40+
{
41+
}

tests/target/item-brace-style-always-next-line.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,32 @@ mod M
4040
where
4141
T: Copy, {}
4242
}
43+
44+
fn function() {}
45+
46+
trait Trait {}
47+
48+
impl<T> Trait for T {}
49+
50+
trait Trait2<T>
51+
where
52+
T: Copy + Display + Write + Read + FromStr,
53+
{
54+
}
55+
56+
trait Trait3<T>
57+
where
58+
T: Something
59+
+ SomethingElse
60+
+ Sync
61+
+ Send
62+
+ Display
63+
+ Debug
64+
+ Copy
65+
+ Hash
66+
+ Debug
67+
+ Display
68+
+ Write
69+
+ Read,
70+
{
71+
}

0 commit comments

Comments
 (0)