Skip to content
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

Preserve semicolon after macro call inside foreign mod #5282

Merged
merged 2 commits into from
Mar 30, 2022
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
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
13 changes: 13 additions & 0 deletions tests/source/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ libc::c_long;
extern {

}

macro_rules! x {
($tt:tt) => {};
}

extern "macros" {
x!(ident);
x!(#);
x![ident];
x![#];
x! {ident}
x! {#}
}
13 changes: 13 additions & 0 deletions tests/target/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ extern "C" {
}

extern "C" {}

macro_rules! x {
($tt:tt) => {};
}

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