@@ -9,8 +9,8 @@ use syntax::{ast, ptr};
9
9
use crate :: chains:: rewrite_chain;
10
10
use crate :: closures;
11
11
use crate :: comment:: {
12
- combine_strs_with_missing_comments, contains_comment , recover_comment_removed , rewrite_comment ,
13
- rewrite_missing_comment, CharClasses , FindUncommented ,
12
+ combine_strs_with_missing_comments, comment_style , contains_comment , recover_comment_removed ,
13
+ rewrite_comment , rewrite_missing_comment, CharClasses , FindUncommented ,
14
14
} ;
15
15
use crate :: config:: lists:: * ;
16
16
use crate :: config:: { Config , ControlBraceStyle , IndentStyle , Version } ;
@@ -808,7 +808,7 @@ impl<'a> ControlFlow<'a> {
808
808
debug ! ( "rewrite_pat_expr {:?} {:?} {:?}" , shape, self . pat, expr) ;
809
809
810
810
let cond_shape = shape. offset_left ( offset) ?;
811
- if ! self . pat . is_none ( ) {
811
+ if let Some ( pat ) = self . pat {
812
812
let matcher = if self . matcher . is_empty ( ) {
813
813
self . matcher . to_owned ( )
814
814
} else {
@@ -817,12 +817,41 @@ impl<'a> ControlFlow<'a> {
817
817
let pat_shape = cond_shape
818
818
. offset_left ( matcher. len ( ) ) ?
819
819
. sub_width ( self . connector . len ( ) ) ?;
820
- let pat_string = if let Some ( pat) = self . pat {
821
- pat. rewrite ( context, pat_shape) ?
820
+ let pat_string = pat. rewrite ( context, pat_shape) ?;
821
+ let comments_lo = context
822
+ . snippet_provider
823
+ . span_after ( self . span , self . connector . trim ( ) ) ;
824
+ let missing_comments = if let Some ( comment) =
825
+ rewrite_missing_comment ( mk_sp ( comments_lo, expr. span . lo ( ) ) , cond_shape, context)
826
+ {
827
+ if !self . connector . is_empty ( ) && !comment. is_empty ( ) {
828
+ if comment_style ( & comment, false ) . is_line_comment ( ) || comment. contains ( "\n " ) {
829
+ let newline = & pat_shape
830
+ . indent
831
+ . block_indent ( context. config )
832
+ . to_string_with_newline ( context. config ) ;
833
+ // An extra space is added when the lhs and rhs are joined
834
+ // so we need to remove one space from the end to ensure
835
+ // the comment and rhs are aligned.
836
+ let mut suffix = newline. as_ref ( ) . to_string ( ) ;
837
+ if !suffix. is_empty ( ) {
838
+ suffix. truncate ( suffix. len ( ) - 1 ) ;
839
+ }
840
+ format ! ( "{}{}{}" , newline, comment, suffix)
841
+ } else {
842
+ format ! ( " {}" , comment)
843
+ }
844
+ } else {
845
+ comment
846
+ }
822
847
} else {
823
848
"" . to_owned ( )
824
849
} ;
825
- let result = format ! ( "{}{}{}" , matcher, pat_string, self . connector) ;
850
+
851
+ let result = format ! (
852
+ "{}{}{}{}" ,
853
+ matcher, pat_string, self . connector, missing_comments
854
+ ) ;
826
855
return rewrite_assign_rhs ( context, result, expr, cond_shape) ;
827
856
}
828
857
0 commit comments