Skip to content

Commit 930d0f7

Browse files
authored
Pick up comments between trait where clause and open block (#4292)
Previously rustfmt only picked up comments between a trait "signature" and its block when there was no `where` clause on the trait. Amending this to work for comments following a `where` clause and preceding the trait block is straightforward; we simply get rid of the branch and adjust the span to search for a comment in accordingly. Closes #4289
1 parent 89192cd commit 930d0f7

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

src/formatting/items.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,24 +1140,28 @@ pub(crate) fn format_trait(
11401140
result.push_str(&where_indent.to_string_with_newline(context.config));
11411141
}
11421142
result.push_str(&where_clause_str);
1143+
}
1144+
let pre_block_span = if !generics.where_clause.predicates.is_empty() {
1145+
mk_sp(generics.where_clause.span.hi(), item.span.hi())
11431146
} else {
1144-
let item_snippet = context.snippet(item.span);
1145-
if let Some(lo) = item_snippet.find('/') {
1146-
// 1 = `{`
1147-
let comment_hi = body_lo - BytePos(1);
1148-
let comment_lo = item.span.lo() + BytePos(lo as u32);
1149-
if comment_lo < comment_hi {
1150-
match recover_missing_comment_in_span(
1151-
mk_sp(comment_lo, comment_hi),
1152-
Shape::indented(offset, context.config),
1153-
context,
1154-
last_line_width(&result),
1155-
) {
1156-
Some(ref missing_comment) if !missing_comment.is_empty() => {
1157-
result.push_str(missing_comment);
1158-
}
1159-
_ => {}
1147+
item.span
1148+
};
1149+
let pre_block_snippet = context.snippet(pre_block_span);
1150+
if let Some(lo) = pre_block_snippet.find('/') {
1151+
// 1 = `{`
1152+
let comment_hi = body_lo - BytePos(1);
1153+
let comment_lo = pre_block_span.lo() + BytePos(lo as u32);
1154+
if comment_lo < comment_hi {
1155+
match recover_missing_comment_in_span(
1156+
mk_sp(comment_lo, comment_hi),
1157+
Shape::indented(offset, context.config),
1158+
context,
1159+
last_line_width(&result),
1160+
) {
1161+
Some(ref missing_comment) if !missing_comment.is_empty() => {
1162+
result.push_str(missing_comment);
11601163
}
1164+
_ => {}
11611165
}
11621166
}
11631167
}

tests/source/issue-4289.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
trait MyTrait
2+
where
3+
Self: Clone, // comment on first constraint
4+
Self: Eq, // comment on last constraint
5+
{
6+
}
7+
8+
trait MyTrait2 where Self: Clone, /* another comment */ {}

tests/target/issue-4289.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait MyTrait
2+
where
3+
Self: Clone, // comment on first constraint
4+
Self: Eq, // comment on last constraint
5+
{
6+
}
7+
8+
trait MyTrait2
9+
where
10+
Self: Clone, /* another comment */
11+
{
12+
}

0 commit comments

Comments
 (0)