Skip to content

Commit

Permalink
Fix rust-lang#5234 - handling of empty code block
Browse files Browse the repository at this point in the history
  • Loading branch information
davidBar-On committed Nov 13, 2022
1 parent ee2bed9 commit 8f51873
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions tests/source/issue-5234.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
47 changes: 47 additions & 0 deletions tests/target/issue-5234.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}

0 comments on commit 8f51873

Please sign in to comment.