Skip to content

Commit 326a4c1

Browse files
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
1 parent 0332da0 commit 326a4c1

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

src/comment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn identify_comment(
256256
config: &Config,
257257
is_doc_comment: bool,
258258
) -> RewriteResult {
259-
let style = comment_style(orig, false);
259+
let style = comment_style(orig.trim_start(), false);
260260

261261
// Computes the byte length of line taking into account a newline if the line is part of a
262262
// paragraph.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-edition: 2024
3+
4+
/// ```
5+
/// println!("1"); // comment
6+
/// # println!("2")
7+
/// ```
8+
struct S;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-edition: 2024
2+
3+
fn wat1() {
4+
println!("1"); // double slash comment
5+
/* another comment type */
6+
}
7+
8+
fn wat1() {
9+
println!("1"); // double slash comment
10+
/* another comment type */
11+
//# yet another type
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-edition: 2024
3+
4+
/// ```
5+
/// println!("1"); // comment
6+
/// # println!("2")
7+
/// ```
8+
struct S;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-edition: 2024
2+
3+
fn wat1() {
4+
println!("1"); // double slash comment
5+
/* another comment type */
6+
}
7+
8+
fn wat1() {
9+
println!("1"); // double slash comment
10+
/* another comment type */
11+
//# yet another type
12+
}

0 commit comments

Comments
 (0)