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

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

Closed
RalfJung opened this issue Jan 3, 2020 · 3 comments · May be fixed by #4745
Closed

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

RalfJung opened this issue Jan 3, 2020 · 3 comments · May be fixed by #4745

Comments

@RalfJung
Copy link
Member

RalfJung commented Jan 3, 2020

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. :/

@calebcartwright
Copy link
Member

calebcartwright commented Jan 3, 2020

Agreed, though I think this is a duplicate of #3255.

I think rustfmt is assuming the same-line comment after the opening { is associated to the first statement in the block, instead of the control flow expression condition, which is why the comment is moved and indented with that first statement

@RalfJung
Copy link
Member Author

RalfJung commented Jan 6, 2020

Yes, this is a duplicate of #3255 indeed.

@RalfJung
Copy link
Member Author

Closing as duplicate of #3255.

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

Successfully merging a pull request may close this issue.

3 participants