From 0d4c1431f58ca10300a323cb4a3b7e472da11551 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Fri, 11 Aug 2023 09:02:04 -0400 Subject: [PATCH] Improve formatting of empty macro_rules! definitions Fixes 5882 --- src/macros.rs | 24 ++++++++++++++++++++++++ tests/source/issue_5882.rs | 7 +++++++ tests/target/issue_5882.rs | 7 +++++++ 3 files changed, 38 insertions(+) create mode 100644 tests/source/issue_5882.rs create mode 100644 tests/target/issue_5882.rs diff --git a/src/macros.rs b/src/macros.rs index e9a298a2769..88d26358b87 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -379,6 +379,23 @@ fn handle_vec_semi( } } +fn rewrite_empty_macro_def_body( + context: &RewriteContext<'_>, + span: Span, + shape: Shape, +) -> Option { + // 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, @@ -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(), diff --git a/tests/source/issue_5882.rs b/tests/source/issue_5882.rs new file mode 100644 index 00000000000..e36f9965438 --- /dev/null +++ b/tests/source/issue_5882.rs @@ -0,0 +1,7 @@ +macro_rules!foo{} +macro_rules!bar{/*comment*/} +macro_rules!baz{//comment +} +macro_rules!foobar{ +//comment +} diff --git a/tests/target/issue_5882.rs b/tests/target/issue_5882.rs new file mode 100644 index 00000000000..565fb434a6a --- /dev/null +++ b/tests/target/issue_5882.rs @@ -0,0 +1,7 @@ +macro_rules! foo {} +macro_rules! bar { /*comment*/ } +macro_rules! baz { //comment +} +macro_rules! foobar { + //comment +}