rust-lang / rust-lang/rustfmt

Rustfmt incorrectly strips comments when removing empty where clause

Open
#4,649 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-comments
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Empty where clauses are semantically valid Rust. However rustfmt incorrectly removes them and comments.

Before:

trait Foo {
    fn bar(&self)
    where
    //     Self: Bar
    // Some comment
    ;
}

fn foo<T>()
where
//     T: Bar,
// Some comment
{
    println!("foo");
}

After:

trait Foo {
    fn bar(&self);
}

fn foo<T>() {
    println!("foo");
}

rustfmt 1.4.25-stable (0f29ff6d 2020-11-11)
I also tested it on master a97fd770e322714933f6f05b6244303c50199dd3

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 two Rust snippets in the report and trace rustfmt's handling of empty where clauses. The fix is complete when formatting preserves the comments in both examples while still handling the empty clauses correctly; add regression coverage where the existing formatter tests for this behavior are found.

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.