Skip to content

Commit a27fa07

Browse files
committed
fixup! Preserve comments in empty statements
1 parent c6fff8e commit a27fa07

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

rustfmt-core/rustfmt-lib/src/stmt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ impl<'a> Stmt<'a> {
5151
result
5252
}
5353

54+
pub(crate) fn is_empty(&self) -> bool {
55+
matches!(self.inner.kind, ast::StmtKind::Empty)
56+
}
57+
5458
fn is_last_expr(&self) -> bool {
5559
if !self.is_last {
5660
return false;

rustfmt-core/rustfmt-lib/src/visitor.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,9 @@ 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())) {
124-
// If the statement is all semicolons, just skip over it. Before that, make sure any
125-
// comment snippet preceding the semicolon is picked up.
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.
126123
let snippet = self.snippet(mk_sp(self.last_pos, stmt.span().lo()));
127124
let original_starts_with_newline = snippet
128125
.find(|c| c != ' ')
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
;
3+
/* extra comment */ ;
4+
}
5+
6+
fn main() {
7+
println!("");
8+
// comment 1
9+
// comment 2
10+
// comment 3
11+
// comment 4
12+
;
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
/* extra comment */
3+
}
4+
5+
fn main() {
6+
println!("");
7+
// comment 1
8+
// comment 2
9+
// comment 3
10+
// comment 4
11+
}

0 commit comments

Comments
 (0)