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

rustfmt removes comment between impl keyword and type name in impl block #5934

Open
leighmcculloch opened this issue Oct 6, 2023 · 3 comments

Comments

@leighmcculloch
Copy link

What version are you using?

Rust / Cargo 1.72.0

What did you do?

  1. Wrote this code:

    impl /* CustomAccountInterface for */ Contract {}
  2. Formatted it with cargo fmt.

What did you expect to see?

impl /* CustomAccountInterface for */ Contract {}

What did you see instead?

Rustfmt removed my code comment.

impl Contract {}

Discussion

Formatting shouldn't be destructive, but removing comments is destructive.

@ytmimi ytmimi changed the title rustfmt shouldn't remove comments from code rustfmt removes comment between impl keyword and type name in impl block Oct 9, 2023
@ytmimi
Copy link
Contributor

ytmimi commented Oct 9, 2023

Thanks for the report! This is another instance of a location where rustfmt isn't expecting to find a comment.

@leighmcculloch
Copy link
Author

Interesting. Comments can appear anywhere and everywhere.

@ytmimi
Copy link
Contributor

ytmimi commented Oct 9, 2023

@leighmcculloch to explain a little further, comments aren't explicitly represented in the AST rustfmt gets back from the rustc parser, and therefore rustfmt needs to peek back into the original source code in order to recover comments. There's comment recovery code for most, but not all locations a comment could exist. For example, comments between the impl keyword and type name in an impl block.

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

No branches or pull requests

2 participants