@@ -20,7 +20,7 @@ use chains::rewrite_chain;
20
20
use closures;
21
21
use codemap:: { LineRangeUtils , SpanUtils } ;
22
22
use comment:: { combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
23
- rewrite_comment, rewrite_missing_comment, FindUncommented } ;
23
+ rewrite_comment, rewrite_missing_comment, CharClasses , FindUncommented } ;
24
24
use config:: { Config , ControlBraceStyle , IndentStyle } ;
25
25
use lists:: { definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
26
26
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting , ListItem , Separator } ;
@@ -2411,20 +2411,20 @@ pub fn wrap_args_with_parens(
2411
2411
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
2412
2412
/// comma from macro can potentially break the code.
2413
2413
fn span_ends_with_comma ( context : & RewriteContext , span : Span ) -> bool {
2414
- let mut encountered_closing_paren = false ;
2415
- for c in context. snippet ( span) . chars ( ) . rev ( ) {
2414
+ let mut result: bool = Default :: default ( ) ;
2415
+ let mut prev_char: char = Default :: default ( ) ;
2416
+
2417
+ for ( kind, c) in CharClasses :: new ( context. snippet ( span) . chars ( ) ) {
2416
2418
match c {
2417
- ',' => return true ,
2418
- ')' => if encountered_closing_paren {
2419
- return false ;
2420
- } else {
2421
- encountered_closing_paren = true ;
2422
- } ,
2423
- _ if c. is_whitespace ( ) => continue ,
2424
- _ => return false ,
2419
+ _ if kind. is_comment ( ) || c. is_whitespace ( ) => continue ,
2420
+ ')' | '}' => result = result && prev_char != c,
2421
+ ',' => result = true ,
2422
+ _ => result = false ,
2425
2423
}
2424
+ prev_char = c;
2426
2425
}
2427
- false
2426
+
2427
+ result
2428
2428
}
2429
2429
2430
2430
fn rewrite_paren ( context : & RewriteContext , subexpr : & ast:: Expr , shape : Shape ) -> Option < String > {
@@ -2608,7 +2608,20 @@ fn rewrite_struct_lit<'a>(
2608
2608
2609
2609
let tactic = struct_lit_tactic ( h_shape, context, & item_vec) ;
2610
2610
let nested_shape = shape_for_tactic ( tactic, h_shape, v_shape) ;
2611
- let fmt = struct_lit_formatting ( nested_shape, tactic, context, base. is_some ( ) ) ;
2611
+
2612
+ let ends_with_comma = span_ends_with_comma ( context, span) ;
2613
+ let force_no_trailing_comma = if context. inside_macro && !ends_with_comma {
2614
+ true
2615
+ } else {
2616
+ false
2617
+ } ;
2618
+
2619
+ let fmt = struct_lit_formatting (
2620
+ nested_shape,
2621
+ tactic,
2622
+ context,
2623
+ force_no_trailing_comma || base. is_some ( ) ,
2624
+ ) ;
2612
2625
2613
2626
write_list ( & item_vec, & fmt) ?
2614
2627
} ;
0 commit comments