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

Fix #5488 - prevent shorthand init for tuple struct #5520

Merged
merged 1 commit into from
Feb 2, 2023

Conversation

RajivTS
Copy link
Contributor

@RajivTS RajivTS commented Aug 27, 2022

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.

src/expr.rs Outdated
Comment on lines 1720 to 1725
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))
}
}
Copy link
Contributor

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)),
        }
        ...

Comment on lines +3 to +11
// 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 };
Copy link
Contributor

@ytmimi ytmimi Aug 28, 2022

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.

Copy link
Contributor

@ytmimi ytmimi left a 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!

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(_));
Copy link
Contributor

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:

Suggested change
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.
Copy link
Contributor

@ytmimi ytmimi left a 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.

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @RajivTS and apologies for both the delayed merge and my missing of your question back on #5488

@calebcartwright calebcartwright added release-notes Needs an associated changelog entry and removed pr-ready-to-merge labels Feb 2, 2023
@calebcartwright calebcartwright merged commit 5391847 into rust-lang:master Feb 2, 2023
@calebcartwright calebcartwright removed the release-notes Needs an associated changelog entry label Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use_field_init_shorthand option breaks numbered struct field initialization
3 participants