Merge pull request #2659 from topecongiro/issue-2652

Do not add a trailing comma on array inside macro
This commit is contained in:
Nick Cameron 2018-04-30 11:07:33 +12:00 committed by GitHub
commit af5976cf1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 11 deletions

View file

@ -70,7 +70,7 @@ pub fn format_expr(
expr.span,
context,
shape,
None,
choose_separator_tactic(context, expr.span),
None,
),
ast::ExprKind::Lit(ref l) => rewrite_literal(context, l, shape),
@ -1336,6 +1336,18 @@ const SPECIAL_MACRO_WHITELIST: &[(&str, usize)] = &[
("debug_assert_ne!", 2),
];
fn choose_separator_tactic(context: &RewriteContext, span: Span) -> Option<SeparatorTactic> {
if context.inside_macro() {
if span_ends_with_comma(context, span) {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
}
} else {
None
}
}
pub fn rewrite_call(
context: &RewriteContext,
callee: &str,
@ -1350,15 +1362,7 @@ pub fn rewrite_call(
shape,
span,
context.config.width_heuristics().fn_call_width,
if context.inside_macro() {
if span_ends_with_comma(context, span) {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
}
} else {
None
},
choose_separator_tactic(context, span),
)
}
@ -1436,11 +1440,14 @@ pub fn is_nested_call(expr: &ast::Expr) -> bool {
pub fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
let mut result: bool = Default::default();
let mut prev_char: char = Default::default();
let closing_delimiters = &[')', '}', ']'];
for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
match c {
_ if kind.is_comment() || c.is_whitespace() => continue,
')' | '}' => result = result && prev_char != ')' && prev_char != '}',
c if closing_delimiters.contains(&c) => {
result &= !closing_delimiters.contains(&prev_char);
}
',' => result = true,
_ => result = false,
}

View file

@ -380,3 +380,11 @@ fn foo() {
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);
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,);
}
// #2652
// Preserve trailing comma inside macro, even if it looks an array.
macro_rules! bar {
($m:ident) => {
$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]);
};
}

View file

@ -961,3 +961,11 @@ fn 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,
);
}
// #2652
// Preserve trailing comma inside macro, even if it looks an array.
macro_rules! bar {
($m:ident) => {
$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]);
};
}