@@ -2365,11 +2365,12 @@ pub fn wrap_args_with_parens(
2365
2365
}
2366
2366
}
2367
2367
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
2369
2369
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
2370
2370
/// comma from macro can potentially break the code.
2371
2371
fn span_ends_with_comma ( context : & RewriteContext , span : Span ) -> bool {
2372
2372
let mut encountered_closing_paren = false ;
2373
+ let mut encountered_closing_braces = false ;
2373
2374
for c in context. snippet ( span) . chars ( ) . rev ( ) {
2374
2375
match c {
2375
2376
',' => return true ,
@@ -2378,6 +2379,11 @@ fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
2378
2379
} else {
2379
2380
encountered_closing_paren = true ;
2380
2381
} ,
2382
+ '}' => if encountered_closing_braces {
2383
+ return false ;
2384
+ } else {
2385
+ encountered_closing_braces = true ;
2386
+ } ,
2381
2387
_ if c. is_whitespace ( ) => continue ,
2382
2388
_ => return false ,
2383
2389
}
@@ -2566,7 +2572,19 @@ fn rewrite_struct_lit<'a>(
2566
2572
2567
2573
let tactic = struct_lit_tactic ( h_shape, context, & item_vec) ;
2568
2574
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
+ ) ;
2570
2588
2571
2589
write_list ( & item_vec, & fmt) ?
2572
2590
} ;
0 commit comments