@@ -20,7 +20,8 @@ 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
+ FullCodeCharKind } ;
24
25
use config:: { Config , ControlBraceStyle , IndentStyle } ;
25
26
use lists:: { definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
26
27
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting , ListItem , Separator } ;
@@ -2365,12 +2366,11 @@ pub fn wrap_args_with_parens(
2365
2366
}
2366
2367
}
2367
2368
2368
- /// Return true if a function call , a method call or a struct represented by the given span ends with a
2369
+ /// Return true if a function call or a method call represented by the given span ends with a
2369
2370
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
2370
2371
/// comma from macro can potentially break the code.
2371
2372
fn span_ends_with_comma ( context : & RewriteContext , span : Span ) -> bool {
2372
2373
let mut encountered_closing_paren = false ;
2373
- let mut encountered_closing_braces = false ;
2374
2374
for c in context. snippet ( span) . chars ( ) . rev ( ) {
2375
2375
match c {
2376
2376
',' => return true ,
@@ -2379,11 +2379,6 @@ fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
2379
2379
} else {
2380
2380
encountered_closing_paren = true ;
2381
2381
} ,
2382
- '}' => if encountered_closing_braces {
2383
- return false ;
2384
- } else {
2385
- encountered_closing_braces = true ;
2386
- } ,
2387
2382
_ if c. is_whitespace ( ) => continue ,
2388
2383
_ => return false ,
2389
2384
}
@@ -2487,6 +2482,26 @@ fn struct_lit_can_be_aligned(fields: &[ast::Field], base: &Option<&ast::Expr>) -
2487
2482
fields. iter ( ) . all ( |field| !field. is_shorthand )
2488
2483
}
2489
2484
2485
+ #[ allow( dead_code) ]
2486
+ /// Check if a struct representation ends with comma by match on the chars of struct
2487
+ /// snippet reversely, once a FullCodeCharKind::Normal comma matched, returns true
2488
+ fn struct_lit_ends_with_comma ( context : & RewriteContext , span : Span ) -> bool {
2489
+ // FIXME: implement DoubleEndedIterator trait for CharClasses
2490
+ //
2491
+ // for (char_kind, c) in CharClasses::new(context.snippet(span).chars()).rev() {
2492
+ // match c {
2493
+ // ',' => match char_kind {
2494
+ // FullCodeCharKind::Normal => return true,
2495
+ // _ => continue,
2496
+ // },
2497
+ // _ => continue,
2498
+ // }
2499
+ // }
2500
+ //
2501
+ // false
2502
+ unimplemented ! ( )
2503
+ }
2504
+
2490
2505
fn rewrite_struct_lit < ' a > (
2491
2506
context : & RewriteContext ,
2492
2507
path : & ast:: Path ,
@@ -2573,12 +2588,13 @@ fn rewrite_struct_lit<'a>(
2573
2588
let tactic = struct_lit_tactic ( h_shape, context, & item_vec) ;
2574
2589
let nested_shape = shape_for_tactic ( tactic, h_shape, v_shape) ;
2575
2590
2576
- let ends_with_comma = span_ends_with_comma ( context, span) ;
2591
+ let ends_with_comma = struct_lit_ends_with_comma ( context, span) ;
2577
2592
let force_no_trailing_comma = if context. inside_macro && !ends_with_comma {
2578
2593
true
2579
2594
} else {
2580
2595
false
2581
2596
} ;
2597
+
2582
2598
let fmt = struct_lit_formatting (
2583
2599
nested_shape,
2584
2600
tactic,
0 commit comments