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

Multiple line addition with comments #5544

Closed
xTachyon opened this issue Sep 18, 2022 · 1 comment
Closed

Multiple line addition with comments #5544

xTachyon opened this issue Sep 18, 2022 · 1 comment

Comments

@xTachyon
Copy link

fn f() {
    const S: usize = 4 + // id
1 + // direction
5; // something
}

I've been trying to explain where my constant comes from with comments, but that seems to break the way this is formatted. I was expecting something more like what clang format does:

void f() {
    constexpr auto S = 4 + // id
                       1 + // direction
                       5;  // something
}

I see now that there's the following error printed when I ask for it, but I don't feel like it's justified. I just want some alignment for the expressions to look a little nicer. I realize that I can put the comment on the next line, but it felt more informative and less easy to forget to update the way I did it.

error[internal]: not formatted because a comment would be lost
 --> \\?\D:\repos\dune\x.rs:2
  |
2 |     const S: usize = 4 + // id
  |
  = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals
@ytmimi
Copy link
Contributor

ytmimi commented Sep 19, 2022

Thanks for reaching out, but going to close this as a duplicate of #3591 and #3167. The issue is that we don't handle comments in binary expressions. rustfmt wants to rewrite your code as const S: usize = 4 + 1 + 5;, but doing so would remove the comment and therefore rustfmt leaves the code unformatted.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 19, 2022
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