Merge pull request #2659 from topecongiro/issue-2652
Do not add a trailing comma on array inside macro
This commit is contained in:
commit
af5976cf1e
3 changed files with 34 additions and 11 deletions
29
src/expr.rs
29
src/expr.rs
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue