diff --git a/src/comment.rs b/src/comment.rs index f9d8a0fa70c..dd195d11cfb 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -607,16 +607,22 @@ impl<'a> CommentRewrite<'a> { cr } - fn join_block(s: &str, sep: &str) -> String { + fn join_block(s: &str, sep: &str, add_sep_prefix: bool) -> String { let mut result = String::with_capacity(s.len() + 128); let mut iter = s.lines().peekable(); - while let Some(line) = iter.next() { - result.push_str(line); - result.push_str(match iter.peek() { - Some(next_line) if next_line.is_empty() => sep.trim_end(), + let get_sep = |line: Option<&&str>| -> &str { + match line { + Some(&"") => sep.trim_end(), Some(..) => sep, None => "", - }); + } + }; + if add_sep_prefix { + result.push_str(get_sep(iter.peek())); + } + while let Some(line) = iter.next() { + result.push_str(line); + result.push_str(get_sep(iter.peek())); } result } @@ -634,10 +640,10 @@ impl<'a> CommentRewrite<'a> { if !self.code_block_buffer.is_empty() { // There is a code block that is not properly enclosed by backticks. // We will leave them untouched. - self.result.push_str(&self.comment_line_separator); self.result.push_str(&Self::join_block( &trim_custom_comment_prefix(&self.code_block_buffer), &self.comment_line_separator, + true, /* add_sep_prefix */ )); } @@ -660,10 +666,12 @@ impl<'a> CommentRewrite<'a> { Some(s) => self.result.push_str(&Self::join_block( &s, &format!("{}{}", self.comment_line_separator, ib.line_start), + false, /* add_sep_prefix */ )), None => self.result.push_str(&Self::join_block( &ib.original_block_as_string(), &self.comment_line_separator, + false, /* add_sep_prefix */ )), }; } @@ -715,10 +723,12 @@ impl<'a> CommentRewrite<'a> { Some(s) => self.result.push_str(&Self::join_block( &s, &format!("{}{}", self.comment_line_separator, ib.line_start), + false, /* add_sep_prefix */ )), None => self.result.push_str(&Self::join_block( &ib.original_block_as_string(), &self.comment_line_separator, + false, /* add_sep_prefix */ )), }; } else if self.code_block_attr.is_some() { @@ -741,9 +751,11 @@ impl<'a> CommentRewrite<'a> { _ => trim_custom_comment_prefix(&self.code_block_buffer), }; if !code_block.is_empty() { - self.result.push_str(&self.comment_line_separator); - self.result - .push_str(&Self::join_block(&code_block, &self.comment_line_separator)); + self.result.push_str(&Self::join_block( + &code_block, + &self.comment_line_separator, + true, /* add_sep_prefix */ + )); } self.code_block_buffer.clear(); self.result.push_str(&self.comment_line_separator); diff --git a/tests/source/issue-4251.rs b/tests/source/issue-4251.rs new file mode 100644 index 00000000000..af54bc6d4c4 --- /dev/null +++ b/tests/source/issue-4251.rs @@ -0,0 +1,6 @@ +// rustfmt-wrap_comments: true + +//! ``` +//! +//! use something; +//! ``` diff --git a/tests/target/issue-4251.rs b/tests/target/issue-4251.rs new file mode 100644 index 00000000000..852cf5cbae8 --- /dev/null +++ b/tests/target/issue-4251.rs @@ -0,0 +1,6 @@ +// rustfmt-wrap_comments: true + +//! ``` +//! +//! use something; +//! ```