Skip to content

Commit e4c9683

Browse files
committed
fix adds a trailing comma to struct-like macro
1 parent 5025a53 commit e4c9683

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/expr.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,11 +2365,12 @@ pub fn wrap_args_with_parens(
23652365
}
23662366
}
23672367

2368-
/// Return true if a function call or a method call represented by the given span ends with a
2368+
/// Return true if a function call ,a method call or a struct represented by the given span ends with a
23692369
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
23702370
/// comma from macro can potentially break the code.
23712371
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
23722372
let mut encountered_closing_paren = false;
2373+
let mut encountered_closing_braces = false;
23732374
for c in context.snippet(span).chars().rev() {
23742375
match c {
23752376
',' => return true,
@@ -2378,6 +2379,11 @@ fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
23782379
} else {
23792380
encountered_closing_paren = true;
23802381
},
2382+
'}' => if encountered_closing_braces {
2383+
return false;
2384+
} else {
2385+
encountered_closing_braces = true;
2386+
},
23812387
_ if c.is_whitespace() => continue,
23822388
_ => return false,
23832389
}
@@ -2566,7 +2572,19 @@ fn rewrite_struct_lit<'a>(
25662572

25672573
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
25682574
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
2569-
let fmt = struct_lit_formatting(nested_shape, tactic, context, base.is_some());
2575+
2576+
let ends_with_comma = span_ends_with_comma(context, span);
2577+
let force_no_trailing_comma = if context.inside_macro && !ends_with_comma {
2578+
true
2579+
} else {
2580+
false
2581+
};
2582+
let fmt = struct_lit_formatting(
2583+
nested_shape,
2584+
tactic,
2585+
context,
2586+
force_no_trailing_comma || base.is_some(),
2587+
);
25702588

25712589
write_list(&item_vec, &fmt)?
25722590
};

0 commit comments

Comments
 (0)