]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
Merge pull request #2090 from topecongiro/issue-2087
[rust.git] / src / expr.rs
index 679fb309f2910f70a665dfe1f5d591856af56fca..9a020646927599c683b1011414e26c77ba39b8b6 100644 (file)
@@ -2365,11 +2365,24 @@ pub fn wrap_args_with_parens(
     }
 }
 
+/// Return true if a function call or a method call represented by the given span ends with a
+/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
+/// comma from macro can potentially break the code.
 fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
-    let snippet = context.snippet(span);
-    snippet
-        .trim_right_matches(|c: char| c == ')' || c.is_whitespace())
-        .ends_with(',')
+    let mut encountered_closing_paren = false;
+    for c in context.snippet(span).chars().rev() {
+        match c {
+            ',' => return true,
+            ')' => if encountered_closing_paren {
+                return false;
+            } else {
+                encountered_closing_paren = true;
+            },
+            _ if c.is_whitespace() => continue,
+            _ => return false,
+        }
+    }
+    false
 }
 
 fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {