Skip to content

Commit f96e56c

Browse files
jonhoonrc
authored andcommitted
Avoid extra comma in vertical single-field struct patterns (#1403)
* Add (failing) test for #1397 * Fix for #1397 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.
1 parent 6be61bc commit f96e56c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/patterns.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use codemap::SpanUtils;
1313
use config::{IndentStyle, MultilineStyle};
1414
use rewrite::{Rewrite, RewriteContext};
1515
use utils::{wrap_str, format_mutability};
16-
use lists::{format_item_list, itemize_list, ListItem, struct_lit_shape, struct_lit_tactic,
17-
shape_for_tactic, struct_lit_formatting, write_list};
16+
use lists::{DefinitiveListTactic, format_item_list, itemize_list, ListItem, struct_lit_shape,
17+
struct_lit_tactic, shape_for_tactic, struct_lit_formatting, write_list};
1818
use expr::{rewrite_unary_prefix, rewrite_pair};
1919
use types::{rewrite_path, PathContext};
2020
use super::Spanned;
@@ -167,7 +167,13 @@ fn rewrite_struct_pat(path: &ast::Path,
167167
fields_str.push_str("..");
168168
} else {
169169
if !fields_str.is_empty() {
170-
fields_str.push_str(", ");
170+
// there are preceeding struct fields being matched on
171+
if fmt.tactic == DefinitiveListTactic::Vertical {
172+
// if the tactic is Vertical, write_list already added a trailing ,
173+
fields_str.push_str(" ");
174+
} else {
175+
fields_str.push_str(", ");
176+
}
171177
}
172178
fields_str.push_str("..");
173179
}

tests/target/issue-1397.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub enum TransactionState {
2+
Committed(i64),
3+
}
4+
5+
pub enum Packet {
6+
Transaction { state: TransactionState },
7+
}
8+
9+
fn baz(p: Packet) {
10+
loop {
11+
loop {
12+
loop {
13+
loop {
14+
if let Packet::Transaction {
15+
state: TransactionState::Committed(ts, ..), ..
16+
} = p {
17+
unreachable!()
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)