Skip to content

Fix misidentification of indented comments#6617

Open
matthewhughes934 wants to merge 1 commit intorust-lang:mainfrom
matthewhughes934:fix-formatting-comments-with-hidden-code
Open

Fix misidentification of indented comments#6617
matthewhughes934 wants to merge 1 commit intorust-lang:mainfrom
matthewhughes934:fix-formatting-comments-with-hidden-code

Conversation

@matthewhughes934
Copy link

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

@jieyouxu jieyouxu added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. UO-format_code_in_doc_comments Unstable option: format_code_in_doc_comments F-impacts-unstable-options Expected formatting impact: only affects code formatted by unstable options and removed pr-not-reviewed labels Feb 19, 2026
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: rust-lang#6612
@matthewhughes934 matthewhughes934 force-pushed the fix-formatting-comments-with-hidden-code branch from 326a4c1 to cada2ef Compare February 25, 2026 19:00
@rustbot
Copy link
Collaborator

rustbot commented Feb 25, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

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

Labels

F-impacts-unstable-options Expected formatting impact: only affects code formatted by unstable options S-waiting-on-review Status: awaiting review from the assignee but also interested parties. UO-format_code_in_doc_comments Unstable option: format_code_in_doc_comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

format_code_in_doc_comments adds empty line between trailing comment and hidden code

3 participants