Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatting empty where clauses when there are return types #6167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2630,29 +2630,38 @@ fn rewrite_fn_base(
}

// Comment between return type and the end of the decl.
let snippet_lo = fd.output.span().hi();
if where_clause.predicates.is_empty() {
let snippet_lo = fd.output.span().hi();
let snippet_hi = span.hi();
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
// Try to preserve the layout of the original snippet.
let original_starts_with_newline = snippet
.find(|c| c != ' ')
.map_or(false, |i| starts_with_newline(&snippet[i..]));
let original_ends_with_newline = snippet
.rfind(|c| c != ' ')
.map_or(false, |i| snippet[i..].ends_with('\n'));
let snippet = snippet.trim();
if !snippet.is_empty() {
result.push(if original_starts_with_newline {
'\n'
} else {
' '
});
result.push_str(snippet);
if original_ends_with_newline {
force_new_line_for_brace = true;

let mut deal_snippet = |snippet: &str| {
// Try to preserve the layout of the original snippet.
let original_starts_with_newline = snippet
.find(|c| c != ' ')
.map_or(false, |i| starts_with_newline(&snippet[i..]));
let original_ends_with_newline = snippet
.rfind(|c| c != ' ')
.map_or(false, |i| snippet[i..].ends_with('\n'));
let snippet = snippet.trim();
if !snippet.is_empty() {
result.push(if original_starts_with_newline {
'\n'
} else {
' '
});
result.push_str(snippet);
if original_ends_with_newline {
force_new_line_for_brace = true;
}
}
}
};

if context.config.version() == crate::Version::Two && where_clause.has_where_token {
deal_snippet(context.snippet(mk_sp(snippet_lo, where_clause.span.lo())));
deal_snippet(context.snippet(mk_sp(where_clause.span.hi(), snippet_hi)));
} else {
deal_snippet(context.snippet(mk_sp(snippet_lo, snippet_hi)));
};
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/source/empty-where-clauses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-version: Two

fn foo() -> () where {}
fn bar() -> () /* comment */ where {}
fn baz() -> () where /* comment */ {}
fn qux() -> () /* comment */ where /* comment */ {}
6 changes: 6 additions & 0 deletions tests/target/empty-where-clauses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-version: Two

fn foo() -> () {}
fn bar() -> () /* comment */ {}
fn baz() -> () /* comment */ {}
fn qux() -> () /* comment */ /* comment */ {}
Loading