Skip to content

Commit af5976c

Browse files
authored
Merge pull request #2659 from topecongiro/issue-2652
Do not add a trailing comma on array inside macro
2 parents dae9fb6 + 43890cf commit af5976c

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-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
}

Diff for: tests/source/macros.rs

+8
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,11 @@ fn foo() {
380380
foo!(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
381381
foo!(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,);
382382
}
383+
384+
// #2652
385+
// Preserve trailing comma inside macro, even if it looks an array.
386+
macro_rules! bar {
387+
($m:ident) => {
388+
$m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]);
389+
};
390+
}

Diff for: tests/target/macros.rs

+8
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,11 @@ fn foo() {
961961
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
962962
);
963963
}
964+
965+
// #2652
966+
// Preserve trailing comma inside macro, even if it looks an array.
967+
macro_rules! bar {
968+
($m:ident) => {
969+
$m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]);
970+
};
971+
}

0 commit comments

Comments
 (0)