Skip to content

Commit

Permalink
fixup! Preserve comments in empty statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed May 20, 2020
1 parent 8225a0d commit a0f6c18
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions rustfmt-core/rustfmt-lib/src/formatting/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ impl<'a> Stmt<'a> {
result
}

pub(crate) fn is_empty(&self) -> bool {
matches!(self.inner.kind, ast::StmtKind::Empty)
}

fn is_last_expr(&self) -> bool {
if !self.is_last {
return false;
Expand Down
9 changes: 3 additions & 6 deletions rustfmt-core/rustfmt-lib/src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
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 the statement is all semicolons, just skip over it. Before that, make sure any
// comment snippet preceding the semicolon is picked up.
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 != ' ')
Expand Down
13 changes: 13 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue-4018.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
;
/* extra comment */ ;
}

fn main() {
println!("");
// comment 1
// comment 2
// comment 3
// comment 4
;
}
11 changes: 11 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-4018.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
/* extra comment */
}

fn main() {
println!("");
// comment 1
// comment 2
// comment 3
// comment 4
}

0 comments on commit a0f6c18

Please sign in to comment.