Skip to content

Commit 4771809

Browse files
committed
formatting empty where clauses when there are return types
1 parent 16f0ecc commit 4771809

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

src/items.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,29 +2630,38 @@ fn rewrite_fn_base(
26302630
}
26312631

26322632
// Comment between return type and the end of the decl.
2633-
let snippet_lo = fd.output.span().hi();
26342633
if where_clause.predicates.is_empty() {
2634+
let snippet_lo = fd.output.span().hi();
26352635
let snippet_hi = span.hi();
2636-
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
2637-
// Try to preserve the layout of the original snippet.
2638-
let original_starts_with_newline = snippet
2639-
.find(|c| c != ' ')
2640-
.map_or(false, |i| starts_with_newline(&snippet[i..]));
2641-
let original_ends_with_newline = snippet
2642-
.rfind(|c| c != ' ')
2643-
.map_or(false, |i| snippet[i..].ends_with('\n'));
2644-
let snippet = snippet.trim();
2645-
if !snippet.is_empty() {
2646-
result.push(if original_starts_with_newline {
2647-
'\n'
2648-
} else {
2649-
' '
2650-
});
2651-
result.push_str(snippet);
2652-
if original_ends_with_newline {
2653-
force_new_line_for_brace = true;
2636+
2637+
let mut deal_snippet = |snippet: &str| {
2638+
// Try to preserve the layout of the original snippet.
2639+
let original_starts_with_newline = snippet
2640+
.find(|c| c != ' ')
2641+
.map_or(false, |i| starts_with_newline(&snippet[i..]));
2642+
let original_ends_with_newline = snippet
2643+
.rfind(|c| c != ' ')
2644+
.map_or(false, |i| snippet[i..].ends_with('\n'));
2645+
let snippet = snippet.trim();
2646+
if !snippet.is_empty() {
2647+
result.push(if original_starts_with_newline {
2648+
'\n'
2649+
} else {
2650+
' '
2651+
});
2652+
result.push_str(snippet);
2653+
if original_ends_with_newline {
2654+
force_new_line_for_brace = true;
2655+
}
26542656
}
2655-
}
2657+
};
2658+
2659+
if context.config.version() == crate::Version::Two && where_clause.has_where_token {
2660+
deal_snippet(context.snippet(mk_sp(snippet_lo, where_clause.span.lo())));
2661+
deal_snippet(context.snippet(mk_sp(where_clause.span.hi(), snippet_hi)));
2662+
} else {
2663+
deal_snippet(context.snippet(mk_sp(snippet_lo, snippet_hi)));
2664+
};
26562665
}
26572666
}
26582667

tests/source/empty-where-clauses.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-version: Two
2+
3+
fn foo() -> () where {}
4+
fn bar() -> () /* comment */ where {}
5+
fn baz() -> () where /* comment */ {}
6+
fn qux() -> () /* comment */ where /* comment */ {}

tests/target/empty-where-clauses.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-version: Two
2+
3+
fn foo() -> () {}
4+
fn bar() -> () /* comment */ {}
5+
fn baz() -> () /* comment */ {}
6+
fn qux() -> () /* comment */ /* comment */ {}

0 commit comments

Comments
 (0)