Skip to content

Commit 0d4c143

Browse files
ytmimicalebcartwright
authored andcommitted
Improve formatting of empty macro_rules! definitions
Fixes 5882
1 parent 9f58224 commit 0d4c143

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/macros.rs

+24
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,23 @@ fn handle_vec_semi(
379379
}
380380
}
381381

382+
fn rewrite_empty_macro_def_body(
383+
context: &RewriteContext<'_>,
384+
span: Span,
385+
shape: Shape,
386+
) -> Option<String> {
387+
// Create an empty, dummy `ast::Block` representing an empty macro body
388+
let block = ast::Block {
389+
stmts: vec![].into(),
390+
id: rustc_ast::node_id::DUMMY_NODE_ID,
391+
rules: ast::BlockCheckMode::Default,
392+
span: span,
393+
tokens: None,
394+
could_be_bare_literal: false,
395+
};
396+
block.rewrite(context, shape)
397+
}
398+
382399
pub(crate) fn rewrite_macro_def(
383400
context: &RewriteContext<'_>,
384401
shape: Shape,
@@ -419,6 +436,13 @@ pub(crate) fn rewrite_macro_def(
419436
shape
420437
};
421438

439+
if parsed_def.branches.len() == 0 {
440+
let lo = context.snippet_provider.span_before(span, "{");
441+
result += " ";
442+
result += &rewrite_empty_macro_def_body(context, span.with_lo(lo), shape)?;
443+
return Some(result);
444+
}
445+
422446
let branch_items = itemize_list(
423447
context.snippet_provider,
424448
parsed_def.branches.iter(),

tests/source/issue_5882.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro_rules!foo{}
2+
macro_rules!bar{/*comment*/}
3+
macro_rules!baz{//comment
4+
}
5+
macro_rules!foobar{
6+
//comment
7+
}

tests/target/issue_5882.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro_rules! foo {}
2+
macro_rules! bar { /*comment*/ }
3+
macro_rules! baz { //comment
4+
}
5+
macro_rules! foobar {
6+
//comment
7+
}

0 commit comments

Comments
 (0)