Skip to content

Fix for handling empty code block in doc comments #4895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions src/formatting/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,13 @@ pub(crate) fn format_code_block(
result
}

if code_snippet.is_empty() {
return Some(FormattedSnippet {
snippet: String::new(),
non_formatted_ranges: vec![],
});
}

// Wrap the given code block with `fn main()` if it does not have one.
let snippet = enclose_in_main_block(code_snippet, config);
let mut result = String::with_capacity(snippet.len());
Expand All @@ -805,32 +812,42 @@ pub(crate) fn format_code_block(
.unwrap_or_else(|| formatted.snippet.len());
let mut is_indented = true;
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
if !is_first {
result.push('\n');
} else {
is_first = false;
}
let trimmed_line = if !is_indented {
line
} else if line.len() > indent_str.len() {
// Make sure that the line has leading whitespaces.
if line.starts_with(indent_str.as_ref()) {
let offset = if config.hard_tabs() {
1

if FN_MAIN_PREFIX.len() >= block_len {
// Code block with empty lines
result.push_str("\n\n");
} else {
// Non-empty code block
for (kind, ref line) in
LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len])
{
if !is_first {
result.push('\n');
} else {
is_first = false;
}
let trimmed_line = if !is_indented {
line
} else if line.len() > indent_str.len() {
// Make sure that the line has leading whitespaces.
if line.starts_with(indent_str.as_ref()) {
let offset = if config.hard_tabs() {
1
} else {
config.tab_spaces()
};
&line[offset..]
} else {
config.tab_spaces()
};
&line[offset..]
line
}
} else {
line
}
} else {
line
};
result.push_str(trimmed_line);
is_indented = indent_next_line(kind);
};
result.push_str(trimmed_line);
is_indented = indent_next_line(kind);
}
}

Some(FormattedSnippet {
snippet: result,
non_formatted_ranges: formatted.non_formatted_ranges,
Expand Down
38 changes: 38 additions & 0 deletions tests/source/issue-4793-empty-code-block-in-doc-comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// rustfmt-format_code_in_doc_comments: true

/// ```
/// ```
fn foo() {}

/// ```
///Something
/// ```
fn foo() {}

/// ```
///
/// ```
fn foo() {}

fn foo() {
/// ```
///
/// ```
struct bar {}
}

/// ```
/// fn com(){
/// let i = 5;
/// }
/// ```
fn foo() {}

fn foo() {
/// ```
///fn com(){
///let i = 5;
///}
/// ```
struct bar {}
}
38 changes: 38 additions & 0 deletions tests/target/issue-4793-empty-code-block-in-doc-comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// rustfmt-format_code_in_doc_comments: true

/// ```
/// ```
fn foo() {}

/// ```
/// Something
/// ```
fn foo() {}

/// ```
///
/// ```
fn foo() {}

fn foo() {
/// ```
///
/// ```
struct bar {}
}

/// ```
/// fn com() {
/// let i = 5;
/// }
/// ```
fn foo() {}

fn foo() {
/// ```
/// fn com() {
/// let i = 5;
/// }
/// ```
struct bar {}
}