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.

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Change itself looks fine, some nits
@rustbot author

View changes since this review

Copy link
Member

Choose a reason for hiding this comment

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

Question [IDEMPOTENCE 1/2]: is this a "rustfmt doesn't change stable formatting" test? If so, can you remove the source/ copy and leave only the target/ copy (as idempotence check is done against the target/ copy)?

Copy link
Author

Choose a reason for hiding this comment

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

👍 thanks, I wasn't aware of a standalone file in target meant it is run as an idempotentcy test, I'll double check that's the case and update accordingly

Copy link
Member

@jieyouxu jieyouxu Mar 6, 2026

Choose a reason for hiding this comment

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

I did some more trial and error myself, I missed that there is specifically a case where you would want both a source/ and target/ copy, which is to ensure idempotence. That is, having both source/ and target/ even if identical helps you catch the case where the target copy is already in canonical form, but that it make take more than 1 rustfmt runs to get from the source copy to target copy.

When there's only a target/ copy, the test is only good for "the format is indeed canonical" and implies no guarantees on idempotency actually, sorry.

(IOW, leaving the source copies as-is is probably good, because other stuff might have strange interations with these examples.)

I'll update the contributing docs because this wasn't super obvious to me at first, but became obvious on hindsight
EDIT: #6821

config: &Config,
is_doc_comment: bool,
) -> RewriteResult {
let style = comment_style(orig, false);
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: can you leave a brief comment here with an example, i.e.

// Account for leading whitespace, e.g. `  //#`

Copy link
Author

Choose a reason for hiding this comment

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

Suggestion: can you leave a brief comment here with an example, i.e.

// Account for leading whitespace, e.g. `  //#`

🤔 let me actually think over this change a bit more: I'm wondering if this is too naive and comments with different indentation levels are now accidentally match on the same style.

Copy link
Member

Choose a reason for hiding this comment

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

Question [IDEMPOTENCE 2/2]: ditto on the "format as expected"

Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: can you also add coverage for:

  1. Doc comment but without the code fenses, i.e.
/// println!("1"); // comment
/// # println!("2")
struct S;
  1. Code block in doc comment but no space between /// and #, i.e.
/// ```
/// println!("1"); // comment
///# println!("2")
/// ```
struct S;
/// ```
/// println!("1"); // comment
///#println!("2")
/// ```
struct S;

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Mar 4, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 4, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@matthewhughes934
Copy link
Author

I just discovered #5392 which is basically doing the same as this PR (but that one has a bit more context). Can we merge that one instead?

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-author Status: awaiting some action (such as code changes or more information) from the author. 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