Avoid extra comma in vertical single-field struct patterns ()

* Add (failing) test for 

* Fix for 

Specifically, we end up double-adding a trailing comma for single-member
struct patterns that are arranged vertically. One is added by write_list
(since such structs return true for needs_trailing_separator), and
another is added by the if in the old code.
This commit is contained in:
Jon Gjengset 2017-03-26 01:16:45 -04:00 committed by Nick Cameron
parent 6be61bcdd6
commit f96e56c3a0
2 changed files with 32 additions and 3 deletions

View File

@ -13,8 +13,8 @@ use codemap::SpanUtils;
use config::{IndentStyle, MultilineStyle};
use rewrite::{Rewrite, RewriteContext};
use utils::{wrap_str, format_mutability};
use lists::{format_item_list, itemize_list, ListItem, struct_lit_shape, struct_lit_tactic,
shape_for_tactic, struct_lit_formatting, write_list};
use lists::{DefinitiveListTactic, format_item_list, itemize_list, ListItem, struct_lit_shape,
struct_lit_tactic, shape_for_tactic, struct_lit_formatting, write_list};
use expr::{rewrite_unary_prefix, rewrite_pair};
use types::{rewrite_path, PathContext};
use super::Spanned;
@ -167,7 +167,13 @@ fn rewrite_struct_pat(path: &ast::Path,
fields_str.push_str("..");
} else {
if !fields_str.is_empty() {
fields_str.push_str(", ");
// there are preceeding struct fields being matched on
if fmt.tactic == DefinitiveListTactic::Vertical {
// if the tactic is Vertical, write_list already added a trailing ,
fields_str.push_str(" ");
} else {
fields_str.push_str(", ");
}
}
fields_str.push_str("..");
}

View File

@ -0,0 +1,23 @@
pub enum TransactionState {
Committed(i64),
}
pub enum Packet {
Transaction { state: TransactionState },
}
fn baz(p: Packet) {
loop {
loop {
loop {
loop {
if let Packet::Transaction {
state: TransactionState::Committed(ts, ..), ..
} = p {
unreachable!()
}
}
}
}
}
}