Skip to content

Commit 43890cf

Browse files
committed
Preserve trailing comma inside array
Closes #2652.
1 parent 121f5e4 commit 43890cf

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

Diff for: src/expr.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn format_expr(
7070
expr.span,
7171
context,
7272
shape,
73-
None,
73+
choose_separator_tactic(context, expr.span),
7474
None,
7575
),
7676
ast::ExprKind::Lit(ref l) => rewrite_literal(context, l, shape),
@@ -1336,6 +1336,18 @@ const SPECIAL_MACRO_WHITELIST: &[(&str, usize)] = &[
13361336
("debug_assert_ne!", 2),
13371337
];
13381338

1339+
fn choose_separator_tactic(context: &RewriteContext, span: Span) -> Option<SeparatorTactic> {
1340+
if context.inside_macro() {
1341+
if span_ends_with_comma(context, span) {
1342+
Some(SeparatorTactic::Always)
1343+
} else {
1344+
Some(SeparatorTactic::Never)
1345+
}
1346+
} else {
1347+
None
1348+
}
1349+
}
1350+
13391351
pub fn rewrite_call(
13401352
context: &RewriteContext,
13411353
callee: &str,
@@ -1350,15 +1362,7 @@ pub fn rewrite_call(
13501362
shape,
13511363
span,
13521364
context.config.width_heuristics().fn_call_width,
1353-
if context.inside_macro() {
1354-
if span_ends_with_comma(context, span) {
1355-
Some(SeparatorTactic::Always)
1356-
} else {
1357-
Some(SeparatorTactic::Never)
1358-
}
1359-
} else {
1360-
None
1361-
},
1365+
choose_separator_tactic(context, span),
13621366
)
13631367
}
13641368

@@ -1436,11 +1440,14 @@ pub fn is_nested_call(expr: &ast::Expr) -> bool {
14361440
pub fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
14371441
let mut result: bool = Default::default();
14381442
let mut prev_char: char = Default::default();
1443+
let closing_delimiters = &[')', '}', ']'];
14391444

14401445
for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
14411446
match c {
14421447
_ if kind.is_comment() || c.is_whitespace() => continue,
1443-
')' | '}' => result = result && prev_char != ')' && prev_char != '}',
1448+
c if closing_delimiters.contains(&c) => {
1449+
result &= !closing_delimiters.contains(&prev_char);
1450+
}
14441451
',' => result = true,
14451452
_ => result = false,
14461453
}

0 commit comments

Comments
 (0)