Merge pull request from topecongiro/issue-2087

Only read the trailing comma of outermost fn call
This commit is contained in:
Nick Cameron 2017-10-27 15:18:13 +05:30 committed by GitHub
commit 90b3222adb
3 changed files with 27 additions and 4 deletions
src
tests
source
target

@ -2365,11 +2365,24 @@ pub fn wrap_args_with_parens(
}
}
/// Return true if a function call or a method call represented by the given span ends with a
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
/// comma from macro can potentially break the code.
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
let snippet = context.snippet(span);
snippet
.trim_right_matches(|c: char| c == ')' || c.is_whitespace())
.ends_with(',')
let mut encountered_closing_paren = false;
for c in context.snippet(span).chars().rev() {
match c {
',' => return true,
')' => if encountered_closing_paren {
return false;
} else {
encountered_closing_paren = true;
},
_ if c.is_whitespace() => continue,
_ => return false,
}
}
false
}
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {

@ -21,6 +21,12 @@ fn main() {
kaas!(/* comments */ a /* post macro */, b /* another */);
trailingcomma!( a , b , c , );
// Preserve trailing comma only when necessary.
ok!(file.seek(
SeekFrom::Start(
table.map(|table| fixture.offset(table)).unwrap_or(0),
)
));
noexpr!( i am not an expression, OK? );

@ -33,6 +33,10 @@ fn main() {
);
trailingcomma!(a, b, c,);
// Preserve trailing comma only when necessary.
ok!(file.seek(SeekFrom::Start(
table.map(|table| fixture.offset(table)).unwrap_or(0),
)));
noexpr!( i am not an expression, OK? );