Merge pull request #1870 from topecongiro/missing-comments-in-fn

Cover comments between function args and the brace
This commit is contained in:
Nick Cameron 2017-08-11 17:07:44 +12:00 committed by GitHub
commit 0ee76bec0f
3 changed files with 80 additions and 0 deletions

View File

@ -2075,6 +2075,40 @@ fn rewrite_fn_base(
pos_before_where,
option,
));
// If there are neither where clause nor return type, we may be missing comments between
// args and `{`.
if where_clause_str.is_empty() {
if let ast::FunctionRetTy::Default(ret_span) = fd.output {
let sp = mk_sp(args_span.hi, ret_span.hi);
let missing_snippet = context.snippet(sp);
let trimmed_snippet = missing_snippet.trim();
let missing_comment = if trimmed_snippet.is_empty() {
String::new()
} else {
try_opt!(rewrite_comment(
trimmed_snippet,
false,
Shape::indented(indent, context.config),
context.config,
))
};
if !missing_comment.is_empty() {
let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
// 1 = ` `
let total_width = missing_comment.len() + last_line_width(&result) + 1;
let force_new_line_before_comment = missing_snippet[..pos].contains('\n') ||
total_width > context.config.max_width();
let sep = if force_new_line_before_comment {
format!("\n{}", indent.to_string(context.config))
} else {
String::from(" ")
};
result.push_str(&sep);
result.push_str(&missing_comment);
force_new_line_for_brace = true;
}
}
}
result.push_str(&where_clause_str);

View File

@ -59,3 +59,26 @@ fn issue_1086() {
* random comment */
fn main() {/* Test */}
// #1643
fn some_fn() /* some comment */
{
}
fn some_fn1()
// some comment
{
}
fn some_fn2() // some comment
{
}
fn some_fn3() /* some comment some comment some comment some comment some comment some comment so */
{
}
fn some_fn4()
/* some comment some comment some comment some comment some comment some comment some comment */
{
}

View File

@ -63,3 +63,26 @@ fn issue_1086() {
fn main() {
// Test
}
// #1643
fn some_fn() // some comment
{
}
fn some_fn1()
// some comment
{
}
fn some_fn2() // some comment
{
}
fn some_fn3() // some comment some comment some comment some comment some comment some comment so
{
}
fn some_fn4()
// some comment some comment some comment some comment some comment some comment some comment
{
}