Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format_code_in_doc_comments removes parts of non-doc comments #4420

Closed
vmx opened this issue Sep 10, 2020 · 3 comments
Closed

format_code_in_doc_comments removes parts of non-doc comments #4420

vmx opened this issue Sep 10, 2020 · 3 comments
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce

Comments

@vmx
Copy link

vmx commented Sep 10, 2020

Describe the bug

All this happens only with format_code_in_doc_comments=true.

A comment gets reformatted, during this reformatting the leading parts are removed.

This is triggered within enums and structs (perhaps other things, I haven't tried). The part that is removed has to follow the comment without a whitespace and start with a square opening bracket [. Everything attached to the bracket (without a whitespace) is removed.

To Reproduce

When running this minimal example with rustfmt --config format_code_in_doc_comments=true

enum Minimal {
    Example,
    //[thisisremoved thatsleft
    // canbeanything
}

The output is:

enum Minimal {
    Example,
    /* thatsleft */
    /* canbeanything */
}

Instead of an enum, the top two lines could also be replaced with a struct, leading to the same buggy behavior:

struct Minimal {
    example: Example,

Expected behavior

I would expect that the code is untouched by rustfmt.

Meta

  • rustfmt version: rustfmt 1.4.18-nightly (c1e9b7b 2020-06-13)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt, but it also happens with cargo fmt
@vmx vmx added the bug Panic, non-idempotency, invalid code, etc. label Sep 10, 2020
@Veetaha
Copy link

Veetaha commented Sep 12, 2020

I'll add that the line comment is reformated to an ugly star-prefixed comment:

enum Foo {
    Bar,
    // a
    // b
}

   ↓ ↓ ↓ ↓ ↓ ↓

enum Foo {
    Bar,
    /* a
     * b */
}

@calebcartwright calebcartwright added a-comments only-with-option requires a non-default option value to reproduce labels Sep 12, 2020
@scampi
Copy link
Contributor

scampi commented Oct 6, 2020

The problem comes from the Custom comment variant in src/formatting/comment.rs: //[thisisremoved is considered as the opener of a custom comment, which is then removed in left_trim_comment_line:

} else if let CommentStyle::Custom(opener) = *style {
if line.starts_with(opener) {
(&line[opener.len()..], true)
} else {
(&line[opener.trim_end().len()..], false)
}

@ytmimi
Copy link
Contributor

ytmimi commented Apr 9, 2022

Going to close this as it was fixed in #5091

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

No branches or pull requests

5 participants