Skip to content

Commit

Permalink
Improve formatting of empty macro_rules! definitions
Browse files Browse the repository at this point in the history
Fixes 5882
  • Loading branch information
ytmimi authored and calebcartwright committed Aug 13, 2023
1 parent 9f58224 commit 0d4c143
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
context: &RewriteContext<'_>,
shape: Shape,
Expand Down Expand Up @@ -419,6 +436,13 @@ pub(crate) fn rewrite_macro_def(
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(
context.snippet_provider,
parsed_def.branches.iter(),
Expand Down
7 changes: 7 additions & 0 deletions tests/source/issue_5882.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro_rules!foo{}
macro_rules!bar{/*comment*/}
macro_rules!baz{//comment
}
macro_rules!foobar{
//comment
}
7 changes: 7 additions & 0 deletions tests/target/issue_5882.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro_rules! foo {}
macro_rules! bar { /*comment*/ }
macro_rules! baz { //comment
}
macro_rules! foobar {
//comment
}

0 comments on commit 0d4c143

Please sign in to comment.