diff --git a/src/items.rs b/src/items.rs index 09b5f7d9d30..06251ffaf76 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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); diff --git a/tests/source/comment.rs b/tests/source/comment.rs index cf40e581b6e..61d7fc454aa 100644 --- a/tests/source/comment.rs +++ b/tests/source/comment.rs @@ -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 */ +{ +} diff --git a/tests/target/comment.rs b/tests/target/comment.rs index 486dde72ff3..9b650dad51f 100644 --- a/tests/target/comment.rs +++ b/tests/target/comment.rs @@ -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 +{ +}