Skip to content

Commit

Permalink
Preserve comments in empty statements
Browse files Browse the repository at this point in the history
Closes #4018
  • Loading branch information
ayazhafiz committed May 20, 2020
1 parent e44a71c commit 8225a0d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion rustfmt-core/rustfmt-lib/src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::formatting::{
utils::{
self, contains_skip, count_newlines, depr_skip_annotation, inner_attributes,
last_line_contains_single_line_comment, last_line_width, mk_sp, ptr_vec_to_ref_vec,
rewrite_ident, stmt_expr,
rewrite_ident, starts_with_newline, stmt_expr,
},
};
use crate::result::{ErrorKind, FormatError};
Expand Down Expand Up @@ -124,6 +124,21 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let is_all_semicolons =
|snippet: &str| snippet.chars().all(|c| c.is_whitespace() || c == ';');
if is_all_semicolons(&self.snippet(stmt.span())) {
// If the statement is all semicolons, 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;
}
Expand Down

0 comments on commit 8225a0d

Please sign in to comment.