about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Bar-On <61089727+davidBar-On@users.noreply.github.com>2023-01-29 20:55:14 +0200
committerGitHub <noreply@github.com>2023-01-29 12:55:14 -0600
commitb08130c59070bb9c95128341d712c0e02bba58d9 (patch)
tree9af37f13d89b6509299760edfaf2ae46d61683b8
parentbcfc57ec8a8fb1120b9b447df21cd07aac6c228d (diff)
downloadrust-b08130c59070bb9c95128341d712c0e02bba58d9.tar.gz
rust-b08130c59070bb9c95128341d712c0e02bba58d9.zip
Fix #5234 - handling of empty code block (#5601)
-rw-r--r--src/comment.rs2
-rw-r--r--tests/source/issue-5234.rs51
-rw-r--r--tests/target/issue-5234.rs47
3 files changed, 99 insertions, 1 deletions
diff --git a/src/comment.rs b/src/comment.rs
index 261fa5e93d8..0677fdc2b89 100644
--- a/src/comment.rs
+++ b/src/comment.rs
@@ -726,7 +726,7 @@ impl<'a> CommentRewrite<'a> {
                 let code_block = match self.code_block_attr.as_ref().unwrap() {
                     CodeBlockAttribute::Rust
                         if self.fmt.config.format_code_in_doc_comments()
-                            && !self.code_block_buffer.is_empty() =>
+                            && !self.code_block_buffer.trim().is_empty() =>
                     {
                         let mut config = self.fmt.config.clone();
                         config.set().wrap_comments(false);
diff --git a/tests/source/issue-5234.rs b/tests/source/issue-5234.rs
new file mode 100644
index 00000000000..67266f485d3
--- /dev/null
+++ b/tests/source/issue-5234.rs
@@ -0,0 +1,51 @@
+// rustfmt-format_code_in_doc_comments: true
+
+/// ```
+/// ```
+fn foo() {}
+
+/// ```
+///Something
+/// ```
+fn foo() {}
+
+/// ```
+///
+/// ```
+fn foo() {}
+
+
+/// /// ```
+fn foo() {}
+
+/// /// ```
+/// ```
+///
+/// ```
+/// ```
+fn foo() {}
+
+fn foo() {
+/// ```
+///
+/// ```
+struct bar {}
+}
+
+/// ```
+/// fn com(){
+/// let i = 5;
+///
+/// let j = 6;
+/// }
+/// ```
+fn foo() {}
+
+fn foo() {
+/// ```
+///fn com(){
+///let i = 5;
+///}
+/// ```
+struct bar {}
+}
diff --git a/tests/target/issue-5234.rs b/tests/target/issue-5234.rs
new file mode 100644
index 00000000000..7ee9e46d1ef
--- /dev/null
+++ b/tests/target/issue-5234.rs
@@ -0,0 +1,47 @@
+// rustfmt-format_code_in_doc_comments: true
+
+/// ```
+/// ```
+fn foo() {}
+
+/// ```
+/// Something
+/// ```
+fn foo() {}
+
+/// ```
+/// ```
+fn foo() {}
+
+/// /// ```
+fn foo() {}
+
+/// /// ```
+/// ```
+/// ```
+/// ```
+fn foo() {}
+
+fn foo() {
+    /// ```
+    /// ```
+    struct bar {}
+}
+
+/// ```
+/// fn com() {
+///     let i = 5;
+///
+///     let j = 6;
+/// }
+/// ```
+fn foo() {}
+
+fn foo() {
+    /// ```
+    /// fn com() {
+    ///     let i = 5;
+    /// }
+    /// ```
+    struct bar {}
+}