@@ -34,6 +34,7 @@ use crate::formatting::{
34
34
vertical:: rewrite_with_alignment,
35
35
visitor:: FmtVisitor ,
36
36
} ;
37
+ use std:: ops:: Add ;
37
38
38
39
const DEFAULT_VISIBILITY : ast:: Visibility = ast:: Visibility {
39
40
kind : ast:: VisibilityKind :: Inherited ,
@@ -2528,34 +2529,6 @@ fn rewrite_fn_base(
2528
2529
} else {
2529
2530
result. push_str ( & ret_str) ;
2530
2531
}
2531
-
2532
- if where_clause. predicates . is_empty ( ) {
2533
- // Comment between return type and the end of the decl.
2534
- // Even if there are no predicates in the where clause, the "where" kw may be present,
2535
- // so start the snippet after it.
2536
- let snippet_lo = where_clause. span . hi ( ) ;
2537
- let snippet_hi = span. hi ( ) ;
2538
- let snippet = context. snippet ( mk_sp ( snippet_lo, snippet_hi) ) ;
2539
- // Try to preserve the layout of the original snippet.
2540
- let original_starts_with_newline = snippet
2541
- . find ( |c| c != ' ' )
2542
- . map_or ( false , |i| starts_with_newline ( & snippet[ i..] ) ) ;
2543
- let original_ends_with_newline = snippet
2544
- . rfind ( |c| c != ' ' )
2545
- . map_or ( false , |i| snippet[ i..] . ends_with ( '\n' ) ) ;
2546
- let snippet = snippet. trim ( ) ;
2547
- if !snippet. is_empty ( ) {
2548
- result. push ( if original_starts_with_newline {
2549
- '\n'
2550
- } else {
2551
- ' '
2552
- } ) ;
2553
- result. push_str ( snippet) ;
2554
- if original_ends_with_newline {
2555
- force_new_line_for_brace = true ;
2556
- }
2557
- }
2558
- }
2559
2532
}
2560
2533
2561
2534
let pos_before_where = match fd. output {
@@ -2585,27 +2558,44 @@ fn rewrite_fn_base(
2585
2558
pos_before_where,
2586
2559
option,
2587
2560
) ?;
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
- _ => { }
2561
+
2562
+ result. push_str ( & where_clause_str) ;
2563
+ if where_clause. predicates . is_empty ( ) {
2564
+ // Comment between return type and the end of the decl.
2565
+ // Even if there are no predicates in the where clause, the "where" kw may be present,
2566
+ // so start the snippet after it.
2567
+ let snippet_lo = where_clause. span . hi ( ) ;
2568
+ let snippet_hi = span. hi ( ) ;
2569
+ let comment_indent = match where_clause. has_where_token {
2570
+ true => shape. indent . add ( context. config . tab_spaces ( ) ) ,
2571
+ false => shape. indent ,
2572
+ } ;
2573
+ let mut comment_shape = shape;
2574
+ comment_shape. indent = comment_indent;
2575
+ let missing_comment = recover_missing_comment_in_span (
2576
+ mk_sp ( snippet_lo, snippet_hi) ,
2577
+ comment_shape,
2578
+ context,
2579
+ comment_indent. width ( ) ,
2580
+ )
2581
+ . unwrap_or ( String :: new ( ) ) ;
2582
+ if !missing_comment. is_empty ( ) {
2583
+ // If the comment is more than one line, start it at the line after where
2584
+ if where_clause. has_where_token {
2585
+ result. push_str ( & * format ! (
2586
+ "\n {}where\n {}" ,
2587
+ ( shape. indent) . to_string( context. config) ,
2588
+ missing_comment. trim_start_matches( '\n' ) ,
2589
+ ) ) ;
2590
+ } else {
2591
+ result. push_str ( & * missing_comment) ;
2603
2592
}
2593
+ force_new_line_for_brace = context
2594
+ . snippet ( mk_sp ( snippet_lo, snippet_hi) )
2595
+ . trim_end_matches ( ' ' )
2596
+ . ends_with ( '\n' ) ;
2604
2597
}
2605
2598
}
2606
-
2607
- result. push_str ( & where_clause_str) ;
2608
-
2609
2599
let ends_with_comment = last_line_contains_single_line_comment ( & result) ;
2610
2600
force_new_line_for_brace |= ends_with_comment;
2611
2601
force_new_line_for_brace |=
0 commit comments