File tree 3 files changed +27
-4
lines changed
3 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -2365,11 +2365,24 @@ 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
2369
+ /// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
2370
+ /// comma from macro can potentially break the code.
2368
2371
fn span_ends_with_comma ( context : & RewriteContext , span : Span ) -> bool {
2369
- let snippet = context. snippet ( span) ;
2370
- snippet
2371
- . trim_right_matches ( |c : char | c == ')' || c. is_whitespace ( ) )
2372
- . ends_with ( ',' )
2372
+ let mut encountered_closing_paren = false ;
2373
+ for c in context. snippet ( span) . chars ( ) . rev ( ) {
2374
+ match c {
2375
+ ',' => return true ,
2376
+ ')' => if encountered_closing_paren {
2377
+ return false ;
2378
+ } else {
2379
+ encountered_closing_paren = true ;
2380
+ } ,
2381
+ _ if c. is_whitespace ( ) => continue ,
2382
+ _ => return false ,
2383
+ }
2384
+ }
2385
+ false
2373
2386
}
2374
2387
2375
2388
fn rewrite_paren ( context : & RewriteContext , subexpr : & ast:: Expr , shape : Shape ) -> Option < String > {
Original file line number Diff line number Diff line change @@ -21,6 +21,12 @@ fn main() {
21
21
kaas ! ( /* comments */ a /* post macro */ , b /* another */ ) ;
22
22
23
23
trailingcomma ! ( a , b , c , ) ;
24
+ // Preserve trailing comma only when necessary.
25
+ ok ! ( file. seek(
26
+ SeekFrom :: Start (
27
+ table. map( |table| fixture. offset( table) ) . unwrap_or( 0 ) ,
28
+ )
29
+ ) ) ;
24
30
25
31
noexpr ! ( i am not an expression, OK ? ) ;
26
32
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ fn main() {
33
33
) ;
34
34
35
35
trailingcomma ! ( a, b, c, ) ;
36
+ // Preserve trailing comma only when necessary.
37
+ ok ! ( file. seek( SeekFrom :: Start (
38
+ table. map( |table| fixture. offset( table) ) . unwrap_or( 0 ) ,
39
+ ) ) ) ;
36
40
37
41
noexpr ! ( i am not an expression, OK ? ) ;
38
42
You can’t perform that action at this time.
0 commit comments