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

Inner comments in binop expressions prevents formatting #5099

Closed
wooster0 opened this issue Nov 20, 2021 · 4 comments
Closed

Inner comments in binop expressions prevents formatting #5099

wooster0 opened this issue Nov 20, 2021 · 4 comments

Comments

@wooster0
Copy link
Contributor

fn                                                      main() {
    let   x    =    512_u16;
    let   byte    =  (  x  % 10   )    as  u8  /* make it printable */   +    b'0'        ;
    dbg!(      char::from(              
          byte));
}

is formatted to

fn main() {
    let x = 512_u16;
    let byte = (  x  % 10   )    as  u8  /* make it printable */   +    b'0';
    dbg!(char::from(byte));
}

but I expected

fn main() {
    let x = 512_u16;
    let byte = (x % 10) as u8 /* make it printable */ + b'0';
    dbg!(char::from(byte));
}
@ytmimi
Copy link
Contributor

ytmimi commented Nov 20, 2021

Thanks for the report!

Could you please include the rustfmt version as well as any non default configuration options you might be using?
Although, I don't think the issue has to do with configuration options it would still be nice to know.

If I had to guess, I'd say that rustfmt isn't explicitly checking to see if a comment exists in that location and for that reason it bails on formatting that line because doing so would drop the comment.

@wooster0
Copy link
Contributor Author

This is rustfmt 1.4.37-stable (59eed8a 2021-11-01) with no rustfmt configurations changed.

it bails on formatting that line because doing so would drop the comment

Good point, for that I've opened another issue: #5100

@calebcartwright
Copy link
Member

rustfmt's inability to handle nested comments contained within a binary expression is a known, and long standing issue. It's also one that's unlikely to be resolved any time soon due the inherent complexity, especially given the tendency for chaining. See #4417 for a previous attempt at support this.

I'd suggest that in general with bin expressions that readability/clarity will be improved with inline comments leading/placed above the expression as opposed to trying to embed comments within, but for those that find themselves absolutely needing embedded comments, just be aware/prepared for no reformatting support of those expressions.

@calebcartwright calebcartwright changed the title Inner /* comment */ prevents formatting Inner comments in binop expressions prevents formatting Jan 26, 2022
@ytmimi
Copy link
Contributor

ytmimi commented Jul 27, 2022

Closing as this is a duplicate of #3167 and #3591

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

3 participants