diff --git a/src/comment.rs b/src/comment.rs index 709031dda44..c25f3be2ce6 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -256,7 +256,7 @@ fn identify_comment( config: &Config, is_doc_comment: bool, ) -> RewriteResult { - let style = comment_style(orig, false); + let style = comment_style(orig.trim_start(), false); // Computes the byte length of line taking into account a newline if the line is part of a // paragraph. diff --git a/tests/source/issue-6612/hidden_code_in_doc_comment.rs b/tests/source/issue-6612/hidden_code_in_doc_comment.rs new file mode 100644 index 00000000000..109ca4a4ca2 --- /dev/null +++ b/tests/source/issue-6612/hidden_code_in_doc_comment.rs @@ -0,0 +1,8 @@ +// rustfmt-format_code_in_doc_comments: true +// rustfmt-edition: 2024 + +/// ``` +/// println!("1"); // comment +/// # println!("2") +/// ``` +struct S; diff --git a/tests/source/issue-6612/mixed_comment_types.rs b/tests/source/issue-6612/mixed_comment_types.rs new file mode 100644 index 00000000000..c6487df51aa --- /dev/null +++ b/tests/source/issue-6612/mixed_comment_types.rs @@ -0,0 +1,12 @@ +// rustfmt-edition: 2024 + +fn wat1() { + println!("1"); // double slash comment + /* another comment type */ +} + +fn wat1() { + println!("1"); // double slash comment + /* another comment type */ + //# yet another type +} diff --git a/tests/target/issue-6612/hidden_code_in_doc_comment.rs b/tests/target/issue-6612/hidden_code_in_doc_comment.rs new file mode 100644 index 00000000000..109ca4a4ca2 --- /dev/null +++ b/tests/target/issue-6612/hidden_code_in_doc_comment.rs @@ -0,0 +1,8 @@ +// rustfmt-format_code_in_doc_comments: true +// rustfmt-edition: 2024 + +/// ``` +/// println!("1"); // comment +/// # println!("2") +/// ``` +struct S; diff --git a/tests/target/issue-6612/mixed_comment_types.rs b/tests/target/issue-6612/mixed_comment_types.rs new file mode 100644 index 00000000000..c6487df51aa --- /dev/null +++ b/tests/target/issue-6612/mixed_comment_types.rs @@ -0,0 +1,12 @@ +// rustfmt-edition: 2024 + +fn wat1() { + println!("1"); // double slash comment + /* another comment type */ +} + +fn wat1() { + println!("1"); // double slash comment + /* another comment type */ + //# yet another type +}