From: Seiichi Uchida Date: Wed, 7 Mar 2018 10:29:42 +0000 (+0900) Subject: Use Option over bool to control trailing comma X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=6ba7c34433ef62f25e566fe2f8729a674d57aa00;p=rust.git Use Option over bool to control trailing comma --- diff --git a/src/expr.rs b/src/expr.rs index 647b81830d4..a38e9b0b8f8 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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) diff --git a/src/items.rs b/src/items.rs index 23f96358d8e..d3286864ce7 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1290,7 +1290,7 @@ fn format_tuple_struct( shape, span, context.config.width_heuristics().fn_call_width, - false, + None, )?; } diff --git a/src/macros.rs b/src/macros.rs index db79fb9d576..bb870e589ee 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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, diff --git a/src/overflow.rs b/src/overflow.rs index b5634def086..294c257d31f 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -33,7 +33,7 @@ pub fn rewrite_with_parens( shape: Shape, span: Span, item_max_width: usize, - force_trailing_comma: bool, + force_separator_tactic: Option, ) -> Option where T: Rewrite + ToExpr + Spanned, @@ -47,7 +47,7 @@ pub fn rewrite_with_parens( "(", ")", item_max_width, - force_trailing_comma, + force_separator_tactic, ).rewrite(shape) } @@ -70,7 +70,7 @@ pub fn rewrite_with_angle_brackets( "<", ">", 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, } 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, ) -> 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() diff --git a/src/patterns.rs b/src/patterns.rs index c6cacc8bff5..54b7e0f9a4c 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -343,7 +343,11 @@ fn rewrite_tuple_pat( shape, span, context.config.max_width(), - add_comma, + if add_comma { + Some(SeparatorTactic::Always) + } else { + None + }, ) }