Skip to content

Don't move comments around, or at least move them up instead of down #3994

Closed
@RalfJung

Description

@RalfJung

For some reason, rustfmt arbitrarily decides to move the comment around in the following snippet:

fn main() {
    if true { // explanation.
        println!("hello");
    }
}

(I am saying "arbitrarily" as this page has no explanation for why this happens)

This becomes:

fn main() {
    if true {
        // explanation.
        println!("hello");
    }
}

This is bad for 2 reasons:

  1. Why even move the comment? Having the explanation for the conditional on the same line as the conditional is perfectly fine, very readable and can help save some vertical space.
  2. If rustfmt for some reason must move the comment, it should be moved up. The comment was explaining the if true, and a comment explaining something should not come after the thing already happened! Currently, rustfmt actively degrades the quality of such comments. The reformatted code looks like the comment is explaining the println!.

I am very worried now how many such comments that were in the compiler are wrong / confusing now because of this. :/

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions