Improve formatting of empty macro_rules! definitions

Fixes 5882
This commit is contained in:
Yacin Tmimi 2023-08-11 09:02:04 -04:00 committed by Caleb Cartwright
parent 9f58224123
commit 0d4c1431f5
3 changed files with 38 additions and 0 deletions

View File

@ -379,6 +379,23 @@ fn handle_vec_semi(
} }
} }
fn rewrite_empty_macro_def_body(
context: &RewriteContext<'_>,
span: Span,
shape: Shape,
) -> Option<String> {
// Create an empty, dummy `ast::Block` representing an empty macro body
let block = ast::Block {
stmts: vec![].into(),
id: rustc_ast::node_id::DUMMY_NODE_ID,
rules: ast::BlockCheckMode::Default,
span: span,
tokens: None,
could_be_bare_literal: false,
};
block.rewrite(context, shape)
}
pub(crate) fn rewrite_macro_def( pub(crate) fn rewrite_macro_def(
context: &RewriteContext<'_>, context: &RewriteContext<'_>,
shape: Shape, shape: Shape,
@ -419,6 +436,13 @@ pub(crate) fn rewrite_macro_def(
shape shape
}; };
if parsed_def.branches.len() == 0 {
let lo = context.snippet_provider.span_before(span, "{");
result += " ";
result += &rewrite_empty_macro_def_body(context, span.with_lo(lo), shape)?;
return Some(result);
}
let branch_items = itemize_list( let branch_items = itemize_list(
context.snippet_provider, context.snippet_provider,
parsed_def.branches.iter(), parsed_def.branches.iter(),

View File

@ -0,0 +1,7 @@
macro_rules!foo{}
macro_rules!bar{/*comment*/}
macro_rules!baz{//comment
}
macro_rules!foobar{
//comment
}

View File

@ -0,0 +1,7 @@
macro_rules! foo {}
macro_rules! bar { /*comment*/ }
macro_rules! baz { //comment
}
macro_rules! foobar {
//comment
}