From dbd89363913b2a35110077fed113aaa82b741079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Wed, 2 Oct 2019 16:58:25 +0200 Subject: [PATCH] fix rust code in comment with a line containing only a hash sign (#3818) --- src/comment.rs | 3 ++- tests/source/issue-3751.rs | 10 ++++++++++ tests/target/issue-3751.rs | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-3751.rs create mode 100644 tests/target/issue-3751.rs 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(); +//! # } +//! ```