Skip to content

Commit 0ee76be

Browse files
authored
Merge pull request #1870 from topecongiro/missing-comments-in-fn
Cover comments between function args and the brace
2 parents e1a0d82 + 0bb9986 commit 0ee76be

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/items.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,40 @@ fn rewrite_fn_base(
20752075
pos_before_where,
20762076
option,
20772077
));
2078+
// If there are neither where clause nor return type, we may be missing comments between
2079+
// args and `{`.
2080+
if where_clause_str.is_empty() {
2081+
if let ast::FunctionRetTy::Default(ret_span) = fd.output {
2082+
let sp = mk_sp(args_span.hi, ret_span.hi);
2083+
let missing_snippet = context.snippet(sp);
2084+
let trimmed_snippet = missing_snippet.trim();
2085+
let missing_comment = if trimmed_snippet.is_empty() {
2086+
String::new()
2087+
} else {
2088+
try_opt!(rewrite_comment(
2089+
trimmed_snippet,
2090+
false,
2091+
Shape::indented(indent, context.config),
2092+
context.config,
2093+
))
2094+
};
2095+
if !missing_comment.is_empty() {
2096+
let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
2097+
// 1 = ` `
2098+
let total_width = missing_comment.len() + last_line_width(&result) + 1;
2099+
let force_new_line_before_comment = missing_snippet[..pos].contains('\n') ||
2100+
total_width > context.config.max_width();
2101+
let sep = if force_new_line_before_comment {
2102+
format!("\n{}", indent.to_string(context.config))
2103+
} else {
2104+
String::from(" ")
2105+
};
2106+
result.push_str(&sep);
2107+
result.push_str(&missing_comment);
2108+
force_new_line_for_brace = true;
2109+
}
2110+
}
2111+
}
20782112

20792113
result.push_str(&where_clause_str);
20802114

tests/source/comment.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,26 @@ fn issue_1086() {
5959
* random comment */
6060

6161
fn main() {/* Test */}
62+
63+
// #1643
64+
fn some_fn() /* some comment */
65+
{
66+
}
67+
68+
fn some_fn1()
69+
// some comment
70+
{
71+
}
72+
73+
fn some_fn2() // some comment
74+
{
75+
}
76+
77+
fn some_fn3() /* some comment some comment some comment some comment some comment some comment so */
78+
{
79+
}
80+
81+
fn some_fn4()
82+
/* some comment some comment some comment some comment some comment some comment some comment */
83+
{
84+
}

tests/target/comment.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,26 @@ fn issue_1086() {
6363
fn main() {
6464
// Test
6565
}
66+
67+
// #1643
68+
fn some_fn() // some comment
69+
{
70+
}
71+
72+
fn some_fn1()
73+
// some comment
74+
{
75+
}
76+
77+
fn some_fn2() // some comment
78+
{
79+
}
80+
81+
fn some_fn3() // some comment some comment some comment some comment some comment some comment so
82+
{
83+
}
84+
85+
fn some_fn4()
86+
// some comment some comment some comment some comment some comment some comment some comment
87+
{
88+
}

0 commit comments

Comments
 (0)