Skip to content

Commit fa7d8de

Browse files
committed
Only read the trailing comma of outermost fn call
1 parent daf4789 commit fa7d8de

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/expr.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,11 +2359,24 @@ pub fn wrap_args_with_parens(
23592359
}
23602360
}
23612361

2362+
/// Return true if a function call or a method call represented by the given span ends with a
2363+
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
2364+
/// comma from macro can potentially break the code.
23622365
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
2363-
let snippet = context.snippet(span);
2364-
snippet
2365-
.trim_right_matches(|c: char| c == ')' || c.is_whitespace())
2366-
.ends_with(',')
2366+
let mut encountered_closing_paren = false;
2367+
for c in context.snippet(span).chars().rev() {
2368+
match c {
2369+
',' => return true,
2370+
')' => if encountered_closing_paren {
2371+
return false;
2372+
} else {
2373+
encountered_closing_paren = true;
2374+
},
2375+
_ if c.is_whitespace() => continue,
2376+
_ => return false,
2377+
}
2378+
}
2379+
false
23672380
}
23682381

23692382
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {

0 commit comments

Comments
 (0)