From 512c8c1edfbc6f94313045d3d6bcadcd42751cd7 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 19 Jun 2017 16:00:04 +0900 Subject: [PATCH] Apply config.trailing_comma wherever possible --- src/expr.rs | 14 ++++++++++++-- src/items.rs | 6 +++--- tests/source/configs-trailing_comma-never.rs | 10 ++++++++++ tests/target/configs-trailing_comma-never.rs | 10 ++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 8ff06f650eb..8abc607d662 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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. diff --git a/src/items.rs b/src/items.rs index b7c26574838..9adecb9be94 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2267,12 +2267,12 @@ fn rewrite_args( 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 { diff --git a/tests/source/configs-trailing_comma-never.rs b/tests/source/configs-trailing_comma-never.rs index 0577f2e5aff..4da3b996f29 100644 --- a/tests/source/configs-trailing_comma-never.rs +++ b/tests/source/configs-trailing_comma-never.rs @@ -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), + } } diff --git a/tests/target/configs-trailing_comma-never.rs b/tests/target/configs-trailing_comma-never.rs index 8f351e8dfc2..ae0e50f96d1 100644 --- a/tests/target/configs-trailing_comma-never.rs +++ b/tests/target/configs-trailing_comma-never.rs @@ -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) + } }