diff --git a/src/comment.rs b/src/comment.rs index 6222c34b1a2d..e725adea6bae 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -823,7 +823,8 @@ fn rewrite_comment_inner( const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### "; fn hide_sharp_behind_comment(s: &str) -> Cow<'_, str> { - if s.trim_start().starts_with("# ") { + let s_trimmed = s.trim(); + if s_trimmed.starts_with("# ") || s_trimmed == "#" { Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s)) } else { Cow::from(s) diff --git a/tests/source/issue-3751.rs b/tests/source/issue-3751.rs new file mode 100644 index 000000000000..1343f80e66e5 --- /dev/null +++ b/tests/source/issue-3751.rs @@ -0,0 +1,10 @@ +// rustfmt-format_code_in_doc_comments: true + +//! Empty pound line +//! +//! ```rust +//! # +//! # fn main() { +//! foo ( ) ; +//! # } +//! ``` diff --git a/tests/target/issue-3751.rs b/tests/target/issue-3751.rs new file mode 100644 index 000000000000..e5a03956e9d0 --- /dev/null +++ b/tests/target/issue-3751.rs @@ -0,0 +1,10 @@ +// rustfmt-format_code_in_doc_comments: true + +//! Empty pound line +//! +//! ```rust +//! # +//! # fn main() { +//! foo(); +//! # } +//! ```