Skip to content

Commit 8f51873

Browse files
committed
Fix rust-lang#5234 - handling of empty code block
1 parent ee2bed9 commit 8f51873

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

src/comment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ impl<'a> CommentRewrite<'a> {
726726
let code_block = match self.code_block_attr.as_ref().unwrap() {
727727
CodeBlockAttribute::Rust
728728
if self.fmt.config.format_code_in_doc_comments()
729-
&& !self.code_block_buffer.is_empty() =>
729+
&& !self.code_block_buffer.trim().is_empty() =>
730730
{
731731
let mut config = self.fmt.config.clone();
732732
config.set().wrap_comments(false);

tests/source/issue-5234.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```
4+
/// ```
5+
fn foo() {}
6+
7+
/// ```
8+
///Something
9+
/// ```
10+
fn foo() {}
11+
12+
/// ```
13+
///
14+
/// ```
15+
fn foo() {}
16+
17+
18+
/// /// ```
19+
fn foo() {}
20+
21+
/// /// ```
22+
/// ```
23+
///
24+
/// ```
25+
/// ```
26+
fn foo() {}
27+
28+
fn foo() {
29+
/// ```
30+
///
31+
/// ```
32+
struct bar {}
33+
}
34+
35+
/// ```
36+
/// fn com(){
37+
/// let i = 5;
38+
///
39+
/// let j = 6;
40+
/// }
41+
/// ```
42+
fn foo() {}
43+
44+
fn foo() {
45+
/// ```
46+
///fn com(){
47+
///let i = 5;
48+
///}
49+
/// ```
50+
struct bar {}
51+
}

tests/target/issue-5234.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```
4+
/// ```
5+
fn foo() {}
6+
7+
/// ```
8+
/// Something
9+
/// ```
10+
fn foo() {}
11+
12+
/// ```
13+
/// ```
14+
fn foo() {}
15+
16+
/// /// ```
17+
fn foo() {}
18+
19+
/// /// ```
20+
/// ```
21+
/// ```
22+
/// ```
23+
fn foo() {}
24+
25+
fn foo() {
26+
/// ```
27+
/// ```
28+
struct bar {}
29+
}
30+
31+
/// ```
32+
/// fn com() {
33+
/// let i = 5;
34+
///
35+
/// let j = 6;
36+
/// }
37+
/// ```
38+
fn foo() {}
39+
40+
fn foo() {
41+
/// ```
42+
/// fn com() {
43+
/// let i = 5;
44+
/// }
45+
/// ```
46+
struct bar {}
47+
}

0 commit comments

Comments
 (0)