Skip to content

Only read the trailing comma of outermost fn call #2090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,11 +2359,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> {
Expand Down
6 changes: 6 additions & 0 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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? );

Expand Down
4 changes: 4 additions & 0 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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? );

Expand Down