use crate::syntux::session::ParseSess;
use crate::utils::{
self, contains_skip, count_newlines, depr_skip_annotation, format_unsafety, inner_attributes,
- last_line_width, mk_sp, ptr_vec_to_ref_vec, rewrite_ident, stmt_expr,
+ last_line_width, mk_sp, ptr_vec_to_ref_vec, rewrite_ident, starts_with_newline, stmt_expr,
};
use crate::{ErrorKind, FormatReport, FormattingError};
self.parse_sess.span_to_debug_info(stmt.span())
);
- // https://github.com/rust-lang/rust/issues/63679.
- let is_all_semicolons =
- |snippet: &str| snippet.chars().all(|c| c.is_whitespace() || c == ';');
- if is_all_semicolons(&self.snippet(stmt.span())) {
+ if stmt.is_empty() {
+ // If the statement is empty, just skip over it. Before that, make sure any comment
+ // snippet preceding the semicolon is picked up.
+ let snippet = self.snippet(mk_sp(self.last_pos, stmt.span().lo()));
+ let original_starts_with_newline = snippet
+ .find(|c| c != ' ')
+ .map_or(false, |i| starts_with_newline(&snippet[i..]));
+ let snippet = snippet.trim();
+ if !snippet.is_empty() {
+ if original_starts_with_newline {
+ self.push_str("\n");
+ }
+ self.push_str(&self.block_indent.to_string(self.config));
+ self.push_str(snippet);
+ }
+
self.last_pos = stmt.span().hi();
return;
}