-
Notifications
You must be signed in to change notification settings - Fork 909
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
use_field_init_shorthand option breaks numbered struct field initialization #5488
Comments
Is there any reason in particular you're trying to initialize it that way instead of the more canonical: let instance = MyStruct(0); The other way is valid syntax of course and we should probably adjust the rule to handle this, but with a tuple struct you'd typically see the aforementioned syntax instead. |
For anyone interested in working on this, think you'll probably want to start around the below. Likely just need to enhance the check to not fall into the shorthand mode if the field name is itself an invalid ident (e.g. Lines 1721 to 1723 in e041c20
|
Ignorance! The syntax I was using worked even though it felt clumsy. Of course there’s a better way and now I know. Glad I could identify the issue though. |
No worries, and thanks for bringing it to our attention! It's something we should handle anyway, so better we know than not know and fortunately there's a readily available (and more idiomatic) option for folks in the interim |
@rustbot claim |
Hi @calebcartwright, just wanted to validate the expectation here before implementing the fix and was hoping you could help. AFAIU if the name of the field is any literal (i.e. numeric values, quoted strings, etc.), we want to avoid falling into the short-hand mode? If the above is true, I am thinking of validating the Please let me know if I am overcomplicating or have missed something obvious. |
Closes rust-lang#5488 Fix for rust-lang#5488. Before applying shorthand initialization for structs, check if identifier is a literal (e.g. tuple struct). If yes, then do not apply short hand initialization. Added test case to validate the changes for the fix.
Closes rust-lang#5488 Fix for rust-lang#5488. Before applying shorthand initialization for structs, check if identifier is a literal (e.g. tuple struct). If yes, then do not apply short hand initialization. Added test case to validate the changes for the fix.
Closes rust-lang#5488 Fix for rust-lang#5488. Before applying shorthand initialization for structs, check if identifier is a literal (e.g. tuple struct). If yes, then do not apply short hand initialization. Added test case to validate the changes for the fix.
If I have a struct type with an implicit field such as
And I attempt to initialize it to 0
With the option "use_field_init_shorthand = true", rustfmt will incorrectly shorten the initialization to:
This code will no longer compile after running rustfmt.
The text was updated successfully, but these errors were encountered: