Skip to content

Rustfmt turns significant line into comment for unexpected doc-comment #4590

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

Closed
SimonSapin opened this issue Dec 14, 2020 · 3 comments
Closed
Labels
a-comments bug Panic, non-idempotency, invalid code, etc.

Comments

@SimonSapin
Copy link

Describe the bug

For some programs with a doc-comment at an unexpected / invalid location, rustfmt moves the following line of significant code into the comment, effectively removing it and changing the meaning of the program.

To Reproduce

example.rs:

fn foo(
    /// doc
    arg: u32,
) {}

rustfmt example.rs --emit stdout

Expected behavior

Even though this program does not compile because doc-comments are invalid at that location, the source should be printed unchanged.

Actual behavior

In the formatted code, the argument has effectively been commented out

fn foo(/// doc arg: u32
) {
}

Meta

  • rustfmt version: rustfmt 1.4.29-nightly (70ce182 2020-12-04)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt command line for minimized steps, VSCode + rust-analyzer extension + "format on save" otherwise
@SimonSapin SimonSapin added the bug Panic, non-idempotency, invalid code, etc. label Dec 14, 2020
@calebcartwright calebcartwright added a-comments 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release labels Dec 14, 2020
@ChinYing-Li
Copy link
Contributor

I can take this one. Test cases added for issue #4245 does not include doc-style and block-style, and indeed rustfmt make the following function signature one-line:

fn test2(
    /* doc */
    arg: u32,
    arg2: u32,
) 

fn foo(
    /// doc
    arg: u32,
)

Unexpectedly, if we put the commend right before the closing brace, the code remain unaffected:

fn test4(
    arg: u32,
    arg2: u32,
    /* doc */
)

@karyon
Copy link
Contributor

karyon commented Oct 22, 2021

This is a duplicate of #4936, which has been fixed by #4985. (I noticed this already works on master and then bisected it).

@karyon
Copy link
Contributor

karyon commented Oct 25, 2021

I couldn't find a corresponding PR on the 2.0 branch, so I'll remove the backport label.

@karyon karyon removed the 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release label Oct 25, 2021
@karyon karyon closed this as completed Oct 25, 2021
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.
Projects
None yet
Development

No branches or pull requests

4 participants