rust-lang / rust-lang/rustfmt

Rustfmt deletes comments in trait declarations

Open
#4,672 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

1x-backport:pending A-comments C-bug
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Describe the bug
I have the following piece of code:

pub trait Foo<'a, 'b>
where
    'a: 'b, // Some random comment
{
}

Which rustfmt reformats to:

pub trait Foo<'a, 'b>
where
    'a: 'b,
{
}

Deleting the comment

To Reproduce

Run cargo fmt with the first code block results in the second code block

Expected behavior

The comment should not be deleted

Meta

  • rustfmt version: rustfmt 1.4.25-stable. Reddit user u/m0rphism also reports it can be reproduced with rustfmt 1.4.9-nightly
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: cargo fmt & from vscode

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the trait declaration shown in the report and run cargo fmt using the reported rustfmt setup. Check whether the inline comment after the lifetime bound is retained; done means cargo fmt no longer deletes that comment.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.