-
Notifications
You must be signed in to change notification settings - Fork 883
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
Fix #5488 - prevent shorthand init for tuple struct #5520
Conversation
src/expr.rs
Outdated
Some(ref e) => { | ||
let is_lit = if let ast::ExprKind::Lit(_) = (*field.expr).kind { | ||
true | ||
} else { | ||
false | ||
}; | ||
if !is_lit && e.as_str() == name && context.config.use_field_init_shorthand() { | ||
Some(attrs_str + name) | ||
} else { | ||
Some(format!("{}{}{}{}", attrs_str, name, separator, e)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a suggestion to minimize the diff. We can check if the expr.kind
is an ast::ExprKind::Lit(_)
before we get to the match expression. Then we can just update the first match arm to include the additional check instead of pulling in the logic from the first and second match arms into one arm.
let is_lit = matches!(expr.kind, ast::ExprKind::Lit(_));
match expr {
Some(ref e) if !is_lit && e.as_str() == name && context.config.use_field_init_shorthand() => {
Some(attrs_str + name)
}
Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),
}
...
// Since MyStruct is a tuple struct, it should not be shorthanded to | ||
// MyStruct { 0 } even if use_field_init_shorthand is enabled. | ||
let instance = MyStruct { 0: 0 }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether MyStruct is a tuple struct or not isn't obvious just by looking at the code snippet. Could you add the tuple struct definition for MyStruct before main to make it 100% clear that MyStruct is a tuple struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your first contribution to rustfmt 🎉
Check out some of the inline comments I left and let me know if anything is unclear!
8386bed
to
f3dca32
Compare
src/expr.rs
Outdated
@@ -1716,9 +1716,11 @@ pub(crate) fn rewrite_field( | |||
let overhead = name.len() + separator.len(); | |||
let expr_shape = shape.offset_left(overhead)?; | |||
let expr = field.expr.rewrite(context, expr_shape); | |||
|
|||
let is_lit = matches!((*field.expr).kind, ast::ExprKind::Lit(_)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A minor nit, but could you rewrite this as:
let is_lit = matches!((*field.expr).kind, ast::ExprKind::Lit(_)); | |
let is_lit = matches!(field.expr.kind, ast::ExprKind::Lit(_)); |
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.
f3dca32
to
ec0f134
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work on this! I think we're good to go here. Will hold off on merging to give @calebcartwright a chance to review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closes #5488
Fix for #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.