@@ -25,7 +25,7 @@ use crate::stmt::Stmt;
2525use crate :: syntux:: session:: ParseSess ;
2626use crate :: utils:: {
2727 self , contains_skip, count_newlines, depr_skip_annotation, format_unsafety, inner_attributes,
28- last_line_width, mk_sp, ptr_vec_to_ref_vec, rewrite_ident, stmt_expr,
28+ last_line_width, mk_sp, ptr_vec_to_ref_vec, rewrite_ident, starts_with_newline , stmt_expr,
2929} ;
3030use crate :: { ErrorKind , FormatReport , FormattingError } ;
3131
@@ -117,10 +117,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
117117 self . parse_sess. span_to_debug_info( stmt. span( ) )
118118 ) ;
119119
120- // https://github.com/rust-lang/rust/issues/63679.
121- let is_all_semicolons =
122- |snippet : & str | snippet. chars ( ) . all ( |c| c. is_whitespace ( ) || c == ';' ) ;
123- if is_all_semicolons ( & self . snippet ( stmt. span ( ) ) ) {
120+ if stmt. is_empty ( ) {
121+ // If the statement is empty, just skip over it. Before that, make sure any comment
122+ // snippet preceding the semicolon is picked up.
123+ let snippet = self . snippet ( mk_sp ( self . last_pos , stmt. span ( ) . lo ( ) ) ) ;
124+ let original_starts_with_newline = snippet
125+ . find ( |c| c != ' ' )
126+ . map_or ( false , |i| starts_with_newline ( & snippet[ i..] ) ) ;
127+ let snippet = snippet. trim ( ) ;
128+ if !snippet. is_empty ( ) {
129+ if original_starts_with_newline {
130+ self . push_str ( "\n " ) ;
131+ }
132+ self . push_str ( & self . block_indent . to_string ( self . config ) ) ;
133+ self . push_str ( snippet) ;
134+ }
135+
124136 self . last_pos = stmt. span ( ) . hi ( ) ;
125137 return ;
126138 }
0 commit comments