Skip to content

Commit 43cf077

Browse files
committed
Keep the comment after where clause that has no predicate (rust-lang#4649)
1 parent a9876e8 commit 43cf077

File tree

7 files changed

+82
-24
lines changed

7 files changed

+82
-24
lines changed

src/formatting/items.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,7 +2528,7 @@ fn rewrite_fn_base(
25282528
} else {
25292529
result.push_str(&ret_str);
25302530
}
2531-
2531+
/*
25322532
if where_clause.predicates.is_empty() {
25332533
// Comment between return type and the end of the decl.
25342534
// Even if there are no predicates in the where clause, the "where" kw may be present,
@@ -2555,7 +2555,7 @@ fn rewrite_fn_base(
25552555
force_new_line_for_brace = true;
25562556
}
25572557
}
2558-
}
2558+
}*/
25592559
}
25602560

25612561
let pos_before_where = match fd.output {
@@ -2585,27 +2585,42 @@ fn rewrite_fn_base(
25852585
pos_before_where,
25862586
option,
25872587
)?;
2588-
// If there are neither where-clause nor return type, we may be missing comments between
2589-
// params and `{`.
2590-
if where_clause_str.is_empty() {
2591-
if let ast::FnRetTy::Default(ret_span) = fd.output {
2592-
match recover_missing_comment_in_span(
2593-
mk_sp(params_span.hi(), ret_span.hi()),
2594-
shape,
2595-
context,
2596-
last_line_width(&result),
2597-
) {
2598-
Some(ref missing_comment) if !missing_comment.is_empty() => {
2599-
result.push_str(missing_comment);
2600-
force_new_line_for_brace = true;
2601-
}
2602-
_ => {}
2588+
2589+
result.push_str(&where_clause_str);
2590+
if where_clause.predicates.is_empty() {
2591+
// Comment between return type and the end of the decl.
2592+
// Even if there are no predicates in the where clause, the "where" kw may be present,
2593+
// so start the snippet after it.
2594+
let snippet_lo = where_clause.span.hi();
2595+
let snippet_hi = span.hi();
2596+
let extra_indent = match where_clause.has_where_token {
2597+
true => Indent::new(context.config.tab_spaces(), 0),
2598+
false => Indent::new(0, 0),
2599+
};
2600+
let missing_comment = recover_missing_comment_in_span(
2601+
mk_sp(snippet_lo, snippet_hi),
2602+
shape,
2603+
context,
2604+
shape.indent.width() + extra_indent.width(),
2605+
)
2606+
.unwrap_or(String::new());
2607+
if !missing_comment.is_empty() {
2608+
// If the comment is more than one line, start it at the line after where
2609+
if where_clause.has_where_token {
2610+
result.push_str(&*format!(
2611+
"\n{}where\n{}",
2612+
(shape.indent + extra_indent).to_string(context.config),
2613+
missing_comment.trim_start_matches('\n'),
2614+
));
2615+
} else {
2616+
result.push_str(&*missing_comment);
26032617
}
2618+
force_new_line_for_brace = context
2619+
.snippet(mk_sp(snippet_lo, snippet_hi))
2620+
.trim_end_matches(' ')
2621+
.ends_with('\n');
26042622
}
26052623
}
2606-
2607-
result.push_str(&where_clause_str);
2608-
26092624
let ends_with_comment = last_line_contains_single_line_comment(&result);
26102625
force_new_line_for_brace |= ends_with_comment;
26112626
force_new_line_for_brace |=

tests/source/issue-4649.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
trait Bar {
2+
fn bar(&self, a: T)
3+
where
4+
// Self: Bar
5+
// Some comment
6+
;
7+
8+
fn bar2(&self, a: T) where /* Self: Bar */ ;
9+
}

tests/source/where-clause.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ pub trait AsUnindented {
6565
fn f<T: AsUnindented<
6666
Output<'static> = U>, U>() where U: AsUnindented<
6767
Output<'static> = i32> {}
68+
69+
fn foo2<T>()
70+
where T: FnOnce()
71+
// Comments
72+
{}
73+
74+
fn foo3<T, U>()
75+
where T: FnOnce(), // Comments
76+
U: FnOnce()
77+
{}

tests/target/issue-1113.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub fn foo() -> fmt::Result
2525
}
2626

2727
pub fn foo() -> fmt::Result /*
28-
*
29-
*
30-
*/
28+
*
29+
*
30+
*/
3131
{
3232
panic!()
3333
}

tests/target/issue-4001.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
fn unit() -> () /* comment */ {
1+
fn unit() -> ()
2+
where
3+
/* comment */ {
24
()
35
}

tests/target/issue-4649.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trait Bar {
2+
fn bar(&self, a: T)
3+
where
4+
// Self: Bar
5+
// Some comment
6+
;
7+
8+
fn bar2(&self, a: T)
9+
where
10+
/* Self: Bar */;
11+
}

tests/target/where-clause.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,14 @@ fn f<T: AsUnindented<Output<'static> = U>, U>()
115115
where U: AsUnindented<Output<'static> = i32>
116116
{
117117
}
118+
119+
fn foo2<T>()
120+
where T: FnOnce() // Comments
121+
{
122+
}
123+
124+
fn foo3<T, U>()
125+
where T: FnOnce(), // Comments
126+
U: FnOnce()
127+
{
128+
}

0 commit comments

Comments
 (0)