Skip to content
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,22 @@ fn format_code_block(
// Remove wrapping main block
formatted.unwrap_code_block();

// If wrapper collapsed to a single line like `fn main() { stmt }`,
// extract the body directly so the wrapper's space before `}`
// does not leak into final snippet.
if let Some(body) = formatted
.snippet
.trim_end_matches('\n')
.strip_prefix(FN_MAIN_PREFIX.trim_end())
.and_then(|s| s.strip_prefix(' '))
.and_then(|s| s.strip_suffix(" }"))
{
return Some(FormattedSnippet {
snippet: body.to_owned(),
non_formatted_ranges: formatted.non_formatted_ranges,
});
}
Comment thread
utakotoba marked this conversation as resolved.
Outdated

// Trim "fn main() {" on the first line and "}" on the last line,
// then unindent the whole code block.
let block_len = formatted
Expand Down
8 changes: 8 additions & 0 deletions tests/target/issue-6832.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-format_code_in_doc_comments: true
// rustfmt-fn_single_line: true
// rustfmt-error_on_unformatted: true
Comment thread
utakotoba marked this conversation as resolved.
Outdated

/// ```rust
/// let font = String::from("hello");
/// ```
fn main() {}
Comment thread
utakotoba marked this conversation as resolved.
Outdated
Loading