Skip to content

Commit cc9b52d

Browse files
committed
formatting empty where clauses when there are return types
1 parent d5f1200 commit cc9b52d

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
@@ -2559,29 +2559,38 @@ fn rewrite_fn_base(
25592559
}
25602560

25612561
// Comment between return type and the end of the decl.
2562-
let snippet_lo = fd.output.span().hi();
25632562
if where_clause.predicates.is_empty() {
2563+
let snippet_lo = fd.output.span().hi();
25642564
let snippet_hi = span.hi();
2565-
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
2566-
// Try to preserve the layout of the original snippet.
2567-
let original_starts_with_newline = snippet
2568-
.find(|c| c != ' ')
2569-
.map_or(false, |i| starts_with_newline(&snippet[i..]));
2570-
let original_ends_with_newline = snippet
2571-
.rfind(|c| c != ' ')
2572-
.map_or(false, |i| snippet[i..].ends_with('\n'));
2573-
let snippet = snippet.trim();
2574-
if !snippet.is_empty() {
2575-
result.push(if original_starts_with_newline {
2576-
'\n'
2577-
} else {
2578-
' '
2579-
});
2580-
result.push_str(snippet);
2581-
if original_ends_with_newline {
2582-
force_new_line_for_brace = true;
2565+
2566+
let mut deal_snippet = |snippet: &str| {
2567+
// Try to preserve the layout of the original snippet.
2568+
let original_starts_with_newline = snippet
2569+
.find(|c| c != ' ')
2570+
.map_or(false, |i| starts_with_newline(&snippet[i..]));
2571+
let original_ends_with_newline = snippet
2572+
.rfind(|c| c != ' ')
2573+
.map_or(false, |i| snippet[i..].ends_with('\n'));
2574+
let snippet = snippet.trim();
2575+
if !snippet.is_empty() {
2576+
result.push(if original_starts_with_newline {
2577+
'\n'
2578+
} else {
2579+
' '
2580+
});
2581+
result.push_str(snippet);
2582+
if original_ends_with_newline {
2583+
force_new_line_for_brace = true;
2584+
}
25832585
}
2584-
}
2586+
};
2587+
2588+
if context.config.version() == Version::Two && where_clause.has_where_token {
2589+
deal_snippet(context.snippet(mk_sp(snippet_lo, where_clause.span.lo())));
2590+
deal_snippet(context.snippet(mk_sp(where_clause.span.hi(), snippet_hi)));
2591+
} else {
2592+
deal_snippet(context.snippet(mk_sp(snippet_lo, snippet_hi)));
2593+
};
25852594
}
25862595
}
25872596

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)