Skip to content

Commit

Permalink
Preserve semicolon after macro call inside foreign mod
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay authored and calebcartwright committed Mar 30, 2022
1 parent 8e94761 commit 5ff7b63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn rewrite_macro_name(
fn return_macro_parse_failure_fallback(
context: &RewriteContext<'_>,
indent: Indent,
position: MacroPosition,
span: Span,
) -> Option<String> {
// Mark this as a failure however we format it
Expand Down Expand Up @@ -140,7 +141,11 @@ fn return_macro_parse_failure_fallback(
));

// Return the snippet unmodified if the macro is not block-like
Some(context.snippet(span).to_owned())
let mut snippet = context.snippet(span).to_owned();
if position == MacroPosition::Item {
snippet.push(';');
}
Some(snippet)
}

pub(crate) fn rewrite_macro(
Expand Down Expand Up @@ -233,7 +238,12 @@ fn rewrite_macro_inner(
} = match parse_macro_args(context, ts, style, is_forced_bracket) {
Some(args) => args,
None => {
return return_macro_parse_failure_fallback(context, shape.indent, mac.span());
return return_macro_parse_failure_fallback(
context,
shape.indent,
position,
mac.span(),
);
}
};

Expand Down
4 changes: 2 additions & 2 deletions tests/source/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ macro_rules! x {

extern "macros" {
x!(ident);
// x!(#); FIXME
x!(#);
x![ident];
// x![#]; FIXME
x![#];
x! {ident}
x! {#}
}
4 changes: 2 additions & 2 deletions tests/target/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ macro_rules! x {

extern "macros" {
x!(ident);
// x!(#); FIXME
x!(#);
x![ident];
// x![#]; FIXME
x![#];
x! {ident}
x! {#}
}

0 comments on commit 5ff7b63

Please sign in to comment.