@@ -25,7 +25,7 @@ use crate::formatting::{
25
25
utils:: {
26
26
self , contains_skip, count_newlines, depr_skip_annotation, inner_attributes,
27
27
last_line_contains_single_line_comment, last_line_width, mk_sp, ptr_vec_to_ref_vec,
28
- rewrite_ident, stmt_expr,
28
+ rewrite_ident, starts_with_newline , stmt_expr,
29
29
} ,
30
30
} ;
31
31
use crate :: result:: { ErrorKind , FormatError } ;
@@ -124,6 +124,21 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
124
124
let is_all_semicolons =
125
125
|snippet : & str | snippet. chars ( ) . all ( |c| c. is_whitespace ( ) || c == ';' ) ;
126
126
if is_all_semicolons ( & self . snippet ( stmt. span ( ) ) ) {
127
+ // If the statement is all semicolons, just skip over it. Before that, make sure any
128
+ // comment snippet preceding the semicolon is picked up.
129
+ let snippet = self . snippet ( mk_sp ( self . last_pos , stmt. span ( ) . lo ( ) ) ) ;
130
+ let original_starts_with_newline = snippet
131
+ . find ( |c| c != ' ' )
132
+ . map_or ( false , |i| starts_with_newline ( & snippet[ i..] ) ) ;
133
+ let snippet = snippet. trim ( ) ;
134
+ if !snippet. is_empty ( ) {
135
+ if original_starts_with_newline {
136
+ self . push_str ( "\n " ) ;
137
+ }
138
+ self . push_str ( & self . block_indent . to_string ( self . config ) ) ;
139
+ self . push_str ( snippet) ;
140
+ }
141
+
127
142
self . last_pos = stmt. span ( ) . hi ( ) ;
128
143
return ;
129
144
}
0 commit comments