diff --git a/src/items.rs b/src/items.rs index 60673d4ebdee..81e86b721969 100644 --- a/src/items.rs +++ b/src/items.rs @@ -649,8 +649,20 @@ pub fn format_impl( } else { context.budget(last_line_width(&result)) }; - let option = WhereClauseOption::snuggled(&ref_and_type); - let where_clause_str = rewrite_where_clause( + + let mut option = WhereClauseOption::snuggled(&ref_and_type); + let snippet = context.snippet(item.span); + let open_pos = snippet.find_uncommented("{")? + 1; + if !contains_comment(&snippet[open_pos..]) + && items.is_empty() + && generics.where_clause.predicates.len() == 1 + { + option.suppress_comma(); + option.snuggle(); + option.compress_where(); + } + + let mut where_clause_str = rewrite_where_clause( context, &generics.where_clause, context.config.brace_style(), @@ -684,6 +696,12 @@ pub fn format_impl( if is_impl_single_line(context, items, &result, &where_clause_str, item)? { result.push_str(&where_clause_str); if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) { + // if the where_clause contains extra comments AND + // there is only one where clause predicate + // recover the suppressed comma in single line where_clause formatting + if generics.where_clause.predicates.len() == 1 { + result.push_str(","); + } result.push_str(&format!("{}{{{}}}", &sep, &sep)); } else { result.push_str(" {}"); @@ -2133,6 +2151,18 @@ impl WhereClauseOption { compress_where: false, } } + + pub fn suppress_comma(&mut self) { + self.suppress_comma = true + } + + pub fn compress_where(&mut self) { + self.compress_where = true + } + + pub fn snuggle(&mut self) { + self.snuggle = true + } } fn rewrite_args( diff --git a/tests/source/configs/where_single_line/true.rs b/tests/source/configs/where_single_line/true.rs index daaab865af21..9de98283b5e5 100644 --- a/tests/source/configs/where_single_line/true.rs +++ b/tests/source/configs/where_single_line/true.rs @@ -22,3 +22,5 @@ where fn lorem() -> T where Ipsum: Eq { // body } + +unsafe impl Sync for Foo where (): Send {} diff --git a/tests/target/configs/where_single_line/true.rs b/tests/target/configs/where_single_line/true.rs index c4a7f5d38020..7f816459e3c6 100644 --- a/tests/target/configs/where_single_line/true.rs +++ b/tests/target/configs/where_single_line/true.rs @@ -26,3 +26,5 @@ fn lorem() -> T where Ipsum: Eq { // body } + +unsafe impl Sync for Foo where (): Send {} diff --git a/tests/target/impls.rs b/tests/target/impls.rs index 2175b5d7bd9d..3b0cb0a30d61 100644 --- a/tests/target/impls.rs +++ b/tests/target/impls.rs @@ -48,11 +48,7 @@ where } } -impl Foo for Bar -where - T: Baz, -{ -} +impl Foo for Bar where T: Baz {} impl Foo for Bar where @@ -133,11 +129,7 @@ mod m { } } - impl PartialEq for S - where - T: PartialEq, - { - } + impl PartialEq for S where T: PartialEq {} } impl