@@ -11,8 +11,9 @@ use tracing::debug;
1111
1212use crate :: attr:: filter_inline_attrs;
1313use crate :: comment:: {
14- FindUncommented , combine_strs_with_missing_comments, contains_comment, is_last_comment_block,
15- recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment,
14+ FindUncommented , combine_strs_with_missing_comments, comment_reaches_end_of_line,
15+ contains_comment, is_last_comment_block, recover_comment_removed,
16+ recover_missing_comment_in_span, rewrite_missing_comment,
1617} ;
1718use crate :: config:: lists:: * ;
1819use crate :: config:: { BraceStyle , Config , IndentStyle , StyleEdition } ;
@@ -2212,6 +2213,17 @@ fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
22122213 }
22132214}
22142215
2216+ struct CommentAfterColon {
2217+ content : Option < String > ,
2218+ reaches_end_of_line : bool ,
2219+ }
2220+
2221+ impl CommentAfterColon {
2222+ fn content ( & self ) -> & str {
2223+ self . content . as_deref ( ) . unwrap_or ( "" )
2224+ }
2225+ }
2226+
22152227/// Recover any missing comments between the param and the type.
22162228///
22172229/// # Returns
@@ -2223,7 +2235,7 @@ fn get_missing_param_comments(
22232235 pat_span : Span ,
22242236 ty_span : Span ,
22252237 shape : Shape ,
2226- ) -> ( String , String ) {
2238+ ) -> ( String , CommentAfterColon ) {
22272239 let missing_comment_span = mk_sp ( pat_span. hi ( ) , ty_span. lo ( ) ) ;
22282240
22292241 let span_before_colon = {
@@ -2246,7 +2258,25 @@ fn get_missing_param_comments(
22462258 let comment_after_colon = rewrite_missing_comment ( span_after_colon, shape, context)
22472259 . ok ( )
22482260 . filter ( |comment| !comment. is_empty ( ) )
2249- . map_or ( String :: new ( ) , |comment| format ! ( "{} " , comment) ) ;
2261+ . map_or (
2262+ CommentAfterColon {
2263+ content : None ,
2264+ reaches_end_of_line : false ,
2265+ } ,
2266+ |comment| {
2267+ let reaches_end_of_line =
2268+ comment_reaches_end_of_line ( span_after_colon, context, false ) ;
2269+ let content = if reaches_end_of_line {
2270+ comment
2271+ } else {
2272+ format ! ( "{} " , comment)
2273+ } ;
2274+ CommentAfterColon {
2275+ content : Some ( content) ,
2276+ reaches_end_of_line,
2277+ }
2278+ } ,
2279+ ) ;
22502280 ( comment_before_colon, comment_after_colon)
22512281}
22522282
@@ -2297,9 +2327,21 @@ impl Rewrite for ast::Param {
22972327 if !is_empty_infer ( & * self . ty , self . pat . span ) {
22982328 let ( before_comment, after_comment) =
22992329 get_missing_param_comments ( context, self . pat . span , self . ty . span , shape) ;
2300- result. push_str ( & before_comment) ;
2301- result. push_str ( colon_spaces ( context. config ) ) ;
2302- result. push_str ( & after_comment) ;
2330+
2331+ // In the specific case of comment after line reaching the end of that line,
2332+ // put the comment on-top to keep it from obscuring the following type
2333+ if after_comment. reaches_end_of_line {
2334+ let mut reordered = after_comment. content ( ) . to_string ( ) ;
2335+ reordered. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
2336+ reordered. push_str ( & result) ;
2337+ result = reordered;
2338+ result. push_str ( & before_comment) ;
2339+ result. push_str ( colon_spaces ( context. config ) ) ;
2340+ } else {
2341+ result. push_str ( & before_comment) ;
2342+ result. push_str ( colon_spaces ( context. config ) ) ;
2343+ result. push_str ( after_comment. content ( ) ) ;
2344+ }
23032345 let overhead = last_line_width ( & result) ;
23042346 let max_width = shape
23052347 . width
@@ -2327,7 +2369,7 @@ impl Rewrite for ast::Param {
23272369 ) ?;
23282370 result. push_str ( & before_comment) ;
23292371 result. push_str ( colon_spaces ( context. config ) ) ;
2330- result. push_str ( & after_comment) ;
2372+ result. push_str ( after_comment. content ( ) ) ;
23312373 let overhead = last_line_width ( & result) ;
23322374 let max_width = shape
23332375 . width
0 commit comments