]> git.lizzy.rs Git - rust.git/commitdiff
Apply config.trailing_comma wherever possible
authortopecongiro <seuchida@gmail.com>
Mon, 19 Jun 2017 07:00:04 +0000 (16:00 +0900)
committertopecongiro <seuchida@gmail.com>
Mon, 19 Jun 2017 07:00:04 +0000 (16:00 +0900)
src/expr.rs
src/items.rs
tests/source/configs-trailing_comma-never.rs
tests/target/configs-trailing_comma-never.rs

index 8ff06f650ebd20b748aa53095acacb6de8b8f1fc..8abc607d6620ee5e762ce8aa83785ed4fdcc2bfe 100644 (file)
@@ -1478,6 +1478,7 @@ fn rewrite_match(
         .codemap
         .span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), "{");
 
+    let arm_num = arms.len();
     for (i, arm) in arms.iter().enumerate() {
         // Make sure we get the stuff between arms.
         let missed_str = if i == 0 {
@@ -1497,12 +1498,21 @@ fn rewrite_match(
 
         let arm_str = arm.rewrite(&context, arm_shape.with_max_width(context.config));
         if let Some(ref arm_str) = arm_str {
-            result.push_str(arm_str);
+            // Trim the trailing comma if necessary.
+            if i == arm_num - 1 && context.config.trailing_comma() == SeparatorTactic::Never &&
+                arm_str.ends_with(',')
+            {
+                result.push_str(&arm_str[0..arm_str.len() - 1])
+            } else {
+                result.push_str(arm_str)
+            }
         } else {
             // We couldn't format the arm, just reproduce the source.
             let snippet = context.snippet(mk_sp(arm_start_pos(arm), arm_end_pos(arm)));
             result.push_str(&snippet);
-            result.push_str(arm_comma(context.config, &arm.body));
+            if context.config.trailing_comma() != SeparatorTactic::Never {
+                result.push_str(arm_comma(context.config, &arm.body))
+            }
         }
     }
     // BytePos(1) = closing match brace.
index b7c265748381da84a5e00f4cd3cf3f04e5619406..9adecb9be946db34d2b64022e1705e6b04a694b8 100644 (file)
@@ -2267,12 +2267,12 @@ enum ArgumentKind<'a> {
         IndentStyle::Block => {
             (
                 indent.block_indent(context.config),
-                SeparatorTactic::Vertical,
+                context.config.trailing_comma(),
                 true,
             )
         }
         IndentStyle::Visual if last_line_ends_with_comment => {
-            (arg_indent, SeparatorTactic::Vertical, true)
+            (arg_indent, context.config.trailing_comma(), true)
         }
         IndentStyle::Visual => (arg_indent, SeparatorTactic::Never, false),
     };
@@ -2564,7 +2564,7 @@ fn rewrite_where_clause_rfc_style(
     let comma_tactic = if suppress_comma {
         SeparatorTactic::Never
     } else {
-        SeparatorTactic::Always
+        context.config.trailing_comma()
     };
 
     let fmt = ListFormatting {
index 0577f2e5affa6d4d4dabeb55ef7a44fbb52aff16..4da3b996f2994fdf3f7020e5b704f6a433108e84 100644 (file)
@@ -10,4 +10,14 @@ fn main() {
         let _ = safe_assert_eq!(reply_req_num, request_num, op);
         return Ok((request_num, op, value));
     }
+
+    // #1710
+    pub struct FileInput {
+        input: StringInput,
+        file_name: OsString,
+    }
+    match len {
+        Some(len) => Ok(new(self.input, self.pos + len)),
+        None => Err(self),
+    }
 }
index 8f351e8dfc2e0c8c85981f1e651cee2edc32a83f..ae0e50f96d18c99ae85e1d6397de9c1c4997edcc 100644 (file)
@@ -22,4 +22,14 @@ fn main() {
         let _ = safe_assert_eq!(reply_req_num, request_num, op);
         return Ok((request_num, op, value));
     }
+
+    // #1710
+    pub struct FileInput {
+        input: StringInput,
+        file_name: OsString
+    }
+    match len {
+        Some(len) => Ok(new(self.input, self.pos + len)),
+        None => Err(self)
+    }
 }