]> git.lizzy.rs Git - rust.git/commitdiff
Use Option<SeparatorTactic> over bool to control trailing comma
authorSeiichi Uchida <seuchida@gmail.com>
Wed, 7 Mar 2018 10:29:42 +0000 (19:29 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Fri, 9 Mar 2018 05:07:43 +0000 (14:07 +0900)
src/expr.rs
src/items.rs
src/macros.rs
src/overflow.rs
src/patterns.rs

index 647b81830d42fa1882c5eb6bd85580baf273591f..a38e9b0b8f876eeb26454b29751c591530030de9 100644 (file)
@@ -1936,9 +1936,13 @@ pub fn rewrite_call(
         span,
         context.config.width_heuristics().fn_call_width,
         if context.inside_macro {
-            span_ends_with_comma(context, span)
+            if span_ends_with_comma(context, span) {
+                Some(SeparatorTactic::Always)
+            } else {
+                Some(SeparatorTactic::Never)
+            }
         } else {
-            false
+            None
         },
     )
 }
@@ -2422,10 +2426,18 @@ pub fn rewrite_tuple<'a, T>(
     debug!("rewrite_tuple {:?}", shape);
     if context.use_block_indent() {
         // We use the same rule as function calls for rewriting tuples.
-        let force_trailing_comma = if context.inside_macro {
-            span_ends_with_comma(context, span)
+        let force_tactic = if context.inside_macro {
+            if span_ends_with_comma(context, span) {
+                Some(SeparatorTactic::Always)
+            } else {
+                Some(SeparatorTactic::Never)
+            }
         } else {
-            items.len() == 1
+            if items.len() == 1 {
+                Some(SeparatorTactic::Always)
+            } else {
+                None
+            }
         };
         overflow::rewrite_with_parens(
             context,
@@ -2434,7 +2446,7 @@ pub fn rewrite_tuple<'a, T>(
             shape,
             span,
             context.config.width_heuristics().fn_call_width,
-            force_trailing_comma,
+            force_tactic,
         )
     } else {
         rewrite_tuple_in_visual_indent_style(context, items, span, shape)
index 23f96358d8e036ba7ee45c4710a7ac9aa967740c..d3286864ce7ddf65f23be83b7b61d7c0f283def2 100644 (file)
@@ -1290,7 +1290,7 @@ fn format_tuple_struct(
             shape,
             span,
             context.config.width_heuristics().fn_call_width,
-            false,
+            None,
         )?;
     }
 
index db79fb9d576dd05988990c658bb75c093cea0972..bb870e589eee51336557056501b7f1eaa580ada3 100644 (file)
@@ -233,7 +233,11 @@ pub fn rewrite_macro(
                 shape,
                 mac.span,
                 context.config.width_heuristics().fn_call_width,
-                trailing_comma,
+                if trailing_comma {
+                    Some(SeparatorTactic::Always)
+                } else {
+                    Some(SeparatorTactic::Never)
+                },
             ).map(|rw| match position {
                 MacroPosition::Item => format!("{};", rw),
                 _ => rw,
index b5634def086a958a1c83c421b08b626061e36f14..294c257d31f49205214ceaf7b6466bfba11ab70d 100644 (file)
@@ -33,7 +33,7 @@ pub fn rewrite_with_parens<T>(
     shape: Shape,
     span: Span,
     item_max_width: usize,
-    force_trailing_comma: bool,
+    force_separator_tactic: Option<SeparatorTactic>,
 ) -> Option<String>
 where
     T: Rewrite + ToExpr + Spanned,
@@ -47,7 +47,7 @@ pub fn rewrite_with_parens<T>(
         "(",
         ")",
         item_max_width,
-        force_trailing_comma,
+        force_separator_tactic,
     ).rewrite(shape)
 }
 
@@ -70,7 +70,7 @@ pub fn rewrite_with_angle_brackets<T>(
         "<",
         ">",
         context.config.max_width(),
-        false,
+        None,
     ).rewrite(shape)
 }
 
@@ -85,7 +85,7 @@ struct Context<'a, T: 'a> {
     span: Span,
     item_max_width: usize,
     one_line_width: usize,
-    force_trailing_comma: bool,
+    force_separator_tactic: Option<SeparatorTactic>,
 }
 
 impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
@@ -98,7 +98,7 @@ pub fn new(
         prefix: &'static str,
         suffix: &'static str,
         item_max_width: usize,
-        force_trailing_comma: bool,
+        force_separator_tactic: Option<SeparatorTactic>,
     ) -> Context<'a, T> {
         // 2 = `( `, 1 = `(`
         let paren_overhead = if context.config.spaces_within_parens_and_brackets() {
@@ -134,7 +134,7 @@ pub fn new(
             suffix,
             item_max_width,
             one_line_width,
-            force_trailing_comma,
+            force_separator_tactic,
         }
     }
 
@@ -336,9 +336,9 @@ fn rewrite_items(&self) -> Option<(bool, String)> {
         let fmt = ListFormatting {
             tactic,
             separator: ",",
-            trailing_separator: if self.force_trailing_comma {
-                SeparatorTactic::Always
-            } else if self.context.inside_macro || !self.context.use_block_indent() {
+            trailing_separator: if let Some(tactic) = self.force_separator_tactic {
+                tactic
+            } else if !self.context.use_block_indent() {
                 SeparatorTactic::Never
             } else {
                 self.context.config.trailing_comma()
index c6cacc8bff5f6af8c560cbe2fcf4e16d1493bb0a..54b7e0f9a4cb003429137bbc5cbd99a32a119bda 100644 (file)
@@ -343,7 +343,11 @@ fn rewrite_tuple_pat(
         shape,
         span,
         context.config.max_width(),
-        add_comma,
+        if add_comma {
+            Some(SeparatorTactic::Always)
+        } else {
+            None
+        },
     )
 }