Skip to content

[Issue #6631] fix normalize comments eats single line comments in code blocks#6924

Open
Ripper53 wants to merge 9 commits into
rust-lang:mainfrom
Ripper53:fix-normalize-comments-eats-single-line-comments-in-code-blocks
Open

[Issue #6631] fix normalize comments eats single line comments in code blocks#6924
Ripper53 wants to merge 9 commits into
rust-lang:mainfrom
Ripper53:fix-normalize-comments-eats-single-line-comments-in-code-blocks

Conversation

@Ripper53
Copy link
Copy Markdown

Fix for Issue 6631

Changes

Check if we are in a code block. If we are within one, use the left_trim_comment_code_line function instead of left_trim_comment_line.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label May 23, 2026
Comment thread src/comment.rs
Comment on lines 916 to +930
line = trim_end_unless_two_whitespaces(line.trim_start(), is_doc_comment);
// Drop old closer.
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
line = line[..(line.len() - 2)].trim_end();
}

line
let code_block_matches = line.matches("```").count();
if code_block_matches != 0 && code_block_matches % 2 == 1 {
is_in_code_block = !is_in_code_block;
left_trim_comment_line(line, &style)
} else if is_in_code_block {
left_trim_comment_code_line(line, &style)
} else {
left_trim_comment_line(line, &style)
}
Copy link
Copy Markdown
Contributor

@joshka joshka May 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This feels subjectively a bit above the sort of complexity I'd want to generally see inside a map block. I'd recommend finding a way to simplify this - likely a small helper method that has an intent revealing name and a short comment.

Not a big deal, mostly it's the shape of this code makes it that to understand what it does and what it should do you have to make inferences about the behavior after reading all of it, rather than starting with a mental model and knowing what the implementing code should do.

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a CodeBlockTracker struct in my new commit. This makes the code block state explicit and easier to understand. I added a bunch of comments defining the expected behavior and a few tests for the new struct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants