From 326a4c17b97a3950ceeb4fbb5a72a3f3d0f29569 Mon Sep 17 00:00:00 2001 From: Matthew Hughes Date: Sat, 26 Jul 2025 18:33:55 +0100 Subject: [PATCH] Fix misidentification of indented comments The comment style detection (i.e. `comment_style`) checks the start of a line to determine the style, so it will misidentify lines starting with spaces, e.g. ` //# some content` would be identified as `CommentStyle::DoubleSlash` and not `CommentStyle::Custom("//# ")` resulting in it determining there to be a comment of `""`, which it then indents and appends a `\n` before writing back the original comment. Fix this by trimming the line before trying to identify its style. Fixes: #6612 --- src/comment.rs | 2 +- .../source/issue-6612/hidden_code_in_doc_comment.rs | 8 ++++++++ tests/source/issue-6612/mixed_comment_types.rs | 12 ++++++++++++ .../target/issue-6612/hidden_code_in_doc_comment.rs | 8 ++++++++ tests/target/issue-6612/mixed_comment_types.rs | 12 ++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-6612/hidden_code_in_doc_comment.rs create mode 100644 tests/source/issue-6612/mixed_comment_types.rs create mode 100644 tests/target/issue-6612/hidden_code_in_doc_comment.rs create mode 100644 tests/target/issue-6612/mixed_comment_types.rs 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 +}