Skip to content

Commit 27d5871

Browse files
authored
Exclude where kw from comment following fn return type (#4175)
rustfmt tries to preserve the comment between a fn return type and the start of the fn body if there are no where clauses following the return type. However, even if there are no where clauses present, the "where" keyword may be. To elide the "where" keyword in this context, just get the comment snippet starting after the where clause span, which always includes the "where" keyword if present. Closes #4001
1 parent e0d2f0a commit 27d5871

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

rustfmt-core/rustfmt-lib/src/items.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2403,9 +2403,11 @@ fn rewrite_fn_base(
24032403
result.push_str(&ret_str);
24042404
}
24052405

2406-
// Comment between return type and the end of the decl.
2407-
let snippet_lo = fd.output.span().hi();
24082406
if where_clause.predicates.is_empty() {
2407+
// Comment between return type and the end of the decl.
2408+
// Even if there are no predicates in the where clause, the "where" kw may be present,
2409+
// so start the snippet after it.
2410+
let snippet_lo = where_clause.span.hi();
24092411
let snippet_hi = span.hi();
24102412
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
24112413
// Try to preserve the layout of the original snippet.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn unit() -> () where /* comment */ {
2+
()
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn unit() -> () /* comment */ {
2+
()
3+
}

0 commit comments

Comments
 (0)