]> git.lizzy.rs Git - rust.git/commitdiff
Preserve comments in empty statements (#4180)
authorhafiz <20735482+ayazhafiz@users.noreply.github.com>
Wed, 20 May 2020 15:39:34 +0000 (10:39 -0500)
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>
Sat, 24 Oct 2020 16:13:00 +0000 (11:13 -0500)
* Preserve comments in empty statements

Closes #4018

* fixup! Preserve comments in empty statements

src/stmt.rs
src/visitor.rs
tests/source/issue-4018.rs [new file with mode: 0644]
tests/target/issue-4018.rs [new file with mode: 0644]

index 807cd9e5fada48d23f47519020bc0e3364d47c04..0b3854425ea5fe816a1c871755c7f1a21a3029bf 100644 (file)
@@ -52,6 +52,10 @@ pub(crate) fn from_ast_nodes<I>(iter: I) -> Vec<Self>
         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;
index 523d4e9fea99d0f2ad8e794923ced44e5b118838..e7fa27aae0c2b55d2c7a1976065904c8152a2d75 100644 (file)
@@ -25,7 +25,7 @@
 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};
 
@@ -117,10 +117,22 @@ fn visit_stmt(&mut self, stmt: &Stmt<'_>) {
             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;
         }
diff --git a/tests/source/issue-4018.rs b/tests/source/issue-4018.rs
new file mode 100644 (file)
index 0000000..9a91dd9
--- /dev/null
@@ -0,0 +1,13 @@
+fn main() {
+    ;
+           /* extra comment */            ;
+}
+
+fn main() {
+    println!("");
+    // comment 1
+    // comment 2
+    // comment 3
+    // comment 4
+    ;
+}
diff --git a/tests/target/issue-4018.rs b/tests/target/issue-4018.rs
new file mode 100644 (file)
index 0000000..cef3be0
--- /dev/null
@@ -0,0 +1,11 @@
+fn main() {
+    /* extra comment */
+}
+
+fn main() {
+    println!("");
+    // comment 1
+    // comment 2
+    // comment 3
+    // comment 4
+}