]> git.lizzy.rs Git - rust.git/commitdiff
Look for trailing comma on array and preserve it inside macro call
authortopecongiro <seuchida@gmail.com>
Thu, 30 Nov 2017 13:14:06 +0000 (22:14 +0900)
committertopecongiro <seuchida@gmail.com>
Thu, 30 Nov 2017 13:14:06 +0000 (22:14 +0900)
src/expr.rs

index e7418562770c49d86e46611b2e63a0f1b3ff403a..49a8479ca0fe9b5642d1419b0348104f0a186658 100644 (file)
@@ -479,7 +479,17 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
         separator: ",",
         trailing_separator: if trailing_comma {
             SeparatorTactic::Always
-        } else if context.inside_macro || context.config.indent_style() == IndentStyle::Visual {
+        } else if context.inside_macro && !exprs.is_empty() {
+            let ends_with_bracket = context.snippet(span).ends_with(']');
+            let bracket_offset = if ends_with_bracket { 1 } else { 0 };
+            let snippet = context.snippet(mk_sp(span.lo(), span.hi() - BytePos(bracket_offset)));
+            let last_char_index = snippet.rfind(|c: char| !c.is_whitespace())?;
+            if &snippet[last_char_index..last_char_index + 1] == "," {
+                SeparatorTactic::Always
+            } else {
+                SeparatorTactic::Never
+            }
+        } else if context.config.indent_style() == IndentStyle::Visual {
             SeparatorTactic::Never
         } else {
             SeparatorTactic::Vertical