diff --git a/src/comment.rs b/src/comment.rs index 05d7310122a..40e42b53ed1 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -1074,16 +1074,26 @@ fn light_rewrite_comment( // `*` in `/*`. let first_non_whitespace = l.find(|c| !char::is_whitespace(c)); let left_trimmed = if let Some(fnw) = first_non_whitespace { - if l.as_bytes()[fnw] == b'*' && fnw > 0 { - &l[fnw - 1..] + if l.as_bytes()[fnw] == b'*' { + Cow::Owned(format!(" {}", &l[fnw..])) } else { - &l[fnw..] + Cow::Borrowed(&l[fnw..]) } } else { - "" + Cow::Borrowed("") }; + // Preserve markdown's double-space line break syntax in doc comment. - trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment) + match left_trimmed { + Cow::Borrowed(left_trimmed) => Cow::Borrowed(trim_end_unless_two_whitespaces( + left_trimmed, + is_doc_comment, + )), + Cow::Owned(left_trimmed) => { + let trimmed = trim_end_unless_two_whitespaces(&left_trimmed, is_doc_comment); + Cow::Owned(trimmed.to_string()) + } + } }) .join(&format!("\n{}", offset.to_string(config))) } diff --git a/tests/source/6639-non-idempotent-block-doc-comments/inner-block-doc-comment.rs b/tests/source/6639-non-idempotent-block-doc-comments/inner-block-doc-comment.rs new file mode 100644 index 00000000000..d46cf3688ec --- /dev/null +++ b/tests/source/6639-non-idempotent-block-doc-comments/inner-block-doc-comment.rs @@ -0,0 +1,9 @@ +/*!First comment. +*/ +pub mod outer { + /*!Second comment. +*/ + pub struct Inner { + pub octets: Vec, + } +} diff --git a/tests/source/6639-non-idempotent-block-doc-comments/naive-outer-block-comment.rs b/tests/source/6639-non-idempotent-block-doc-comments/naive-outer-block-comment.rs new file mode 100644 index 00000000000..581dea9e3ab --- /dev/null +++ b/tests/source/6639-non-idempotent-block-doc-comments/naive-outer-block-comment.rs @@ -0,0 +1,3 @@ +/** +*/ +mod foo {} diff --git a/tests/source/6639-non-idempotent-block-doc-comments/outer-block-doc-comment.rs b/tests/source/6639-non-idempotent-block-doc-comments/outer-block-doc-comment.rs new file mode 100644 index 00000000000..b1c8e1c8518 --- /dev/null +++ b/tests/source/6639-non-idempotent-block-doc-comments/outer-block-doc-comment.rs @@ -0,0 +1,13 @@ +/**First comment. +* +*/ +pub mod outer { + /**Second comment. +foo +*/ + pub struct Inner { + /**Third comment. +*/ + pub octets: Vec, + } +} diff --git a/tests/target/6639-non-idempotent-block-doc-comments/inner-block-doc-comment.rs b/tests/target/6639-non-idempotent-block-doc-comments/inner-block-doc-comment.rs new file mode 100644 index 00000000000..660b855dadd --- /dev/null +++ b/tests/target/6639-non-idempotent-block-doc-comments/inner-block-doc-comment.rs @@ -0,0 +1,9 @@ +/*!First comment. + */ +pub mod outer { + /*!Second comment. + */ + pub struct Inner { + pub octets: Vec, + } +} diff --git a/tests/target/6639-non-idempotent-block-doc-comments/naive-outer-block-comment.rs b/tests/target/6639-non-idempotent-block-doc-comments/naive-outer-block-comment.rs new file mode 100644 index 00000000000..42f39ee29ae --- /dev/null +++ b/tests/target/6639-non-idempotent-block-doc-comments/naive-outer-block-comment.rs @@ -0,0 +1,3 @@ +/** + */ +mod foo {} diff --git a/tests/target/6639-non-idempotent-block-doc-comments/outer-block-doc-comment.rs b/tests/target/6639-non-idempotent-block-doc-comments/outer-block-doc-comment.rs new file mode 100644 index 00000000000..13bd081a846 --- /dev/null +++ b/tests/target/6639-non-idempotent-block-doc-comments/outer-block-doc-comment.rs @@ -0,0 +1,13 @@ +/**First comment. + * + */ +pub mod outer { + /**Second comment. + foo + */ + pub struct Inner { + /**Third comment. + */ + pub octets: Vec, + } +}