Skip to content

Commit 21a49c7

Browse files
rchaser53topecongiro
authored andcommitted
not to remove field literal for tuple (#3993)
1 parent 5eb06d0 commit 21a49c7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/expr.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,9 +1703,18 @@ pub(crate) fn rewrite_field(
17031703
let overhead = name.len() + separator.len();
17041704
let expr_shape = shape.offset_left(overhead)?;
17051705
let expr = field.expr.rewrite(context, expr_shape);
1706+
let is_tuple_field = if let ast::ExprKind::Lit(ref lit) = field.expr.kind {
1707+
lit.kind.is_numeric()
1708+
} else {
1709+
false
1710+
};
17061711

17071712
match expr {
1708-
Some(ref e) if e.as_str() == name && context.config.use_field_init_shorthand() => {
1713+
Some(ref e)
1714+
if e.as_str() == name
1715+
&& context.config.use_field_init_shorthand()
1716+
&& !is_tuple_field =>
1717+
{
17091718
Some(attrs_str + name)
17101719
}
17111720
Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),

tests/target/issue-3992.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-use_field_init_shorthand: true
2+
struct TupleStruct(i32);
3+
4+
fn main() {
5+
TupleStruct { 0: 0 };
6+
}

0 commit comments

Comments
 (0)