Skip to content

Commit d9149fb

Browse files
authored
Merge pull request #2741 from csmoe/where_brace
Remove newline in empty impl
2 parents 9f535de + faa4116 commit d9149fb

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

src/items.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,20 @@ pub fn format_impl(
649649
} else {
650650
context.budget(last_line_width(&result))
651651
};
652-
let option = WhereClauseOption::snuggled(&ref_and_type);
653-
let where_clause_str = rewrite_where_clause(
652+
653+
let mut option = WhereClauseOption::snuggled(&ref_and_type);
654+
let snippet = context.snippet(item.span);
655+
let open_pos = snippet.find_uncommented("{")? + 1;
656+
if !contains_comment(&snippet[open_pos..])
657+
&& items.is_empty()
658+
&& generics.where_clause.predicates.len() == 1
659+
{
660+
option.suppress_comma();
661+
option.snuggle();
662+
option.compress_where();
663+
}
664+
665+
let mut where_clause_str = rewrite_where_clause(
654666
context,
655667
&generics.where_clause,
656668
context.config.brace_style(),
@@ -684,6 +696,12 @@ pub fn format_impl(
684696
if is_impl_single_line(context, items, &result, &where_clause_str, item)? {
685697
result.push_str(&where_clause_str);
686698
if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) {
699+
// if the where_clause contains extra comments AND
700+
// there is only one where clause predicate
701+
// recover the suppressed comma in single line where_clause formatting
702+
if generics.where_clause.predicates.len() == 1 {
703+
result.push_str(",");
704+
}
687705
result.push_str(&format!("{}{{{}}}", &sep, &sep));
688706
} else {
689707
result.push_str(" {}");
@@ -2133,6 +2151,18 @@ impl WhereClauseOption {
21332151
compress_where: false,
21342152
}
21352153
}
2154+
2155+
pub fn suppress_comma(&mut self) {
2156+
self.suppress_comma = true
2157+
}
2158+
2159+
pub fn compress_where(&mut self) {
2160+
self.compress_where = true
2161+
}
2162+
2163+
pub fn snuggle(&mut self) {
2164+
self.snuggle = true
2165+
}
21362166
}
21372167

21382168
fn rewrite_args(

tests/source/configs/where_single_line/true.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ where
2222
fn lorem<Ipsum, Dolor, Sit, Amet>() -> T where Ipsum: Eq {
2323
// body
2424
}
25+
26+
unsafe impl Sync for Foo where (): Send {}

tests/target/configs/where_single_line/true.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
2626
where Ipsum: Eq {
2727
// body
2828
}
29+
30+
unsafe impl Sync for Foo where (): Send {}

tests/target/impls.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ where
4848
}
4949
}
5050

51-
impl<T> Foo for Bar<T>
52-
where
53-
T: Baz,
54-
{
55-
}
51+
impl<T> Foo for Bar<T> where T: Baz {}
5652

5753
impl<T> Foo for Bar<T>
5854
where
@@ -133,11 +129,7 @@ mod m {
133129
}
134130
}
135131

136-
impl<T> PartialEq for S<T>
137-
where
138-
T: PartialEq,
139-
{
140-
}
132+
impl<T> PartialEq for S<T> where T: PartialEq {}
141133
}
142134

143135
impl<BorrowType, K, V, NodeType, HandleType>

0 commit comments

Comments
 (0)