Skip to content

outer macro skip attribute on out-of-line module ignored; can't use inner attribute #5324

Closed
@XrXr

Description

@XrXr

I have a macro the invocation of which I want rustfmt to skip. Since the macro expands to top level items, I tried to use #![rustfmt::skip::macros(items)] at the module level to do this. That unfortunately breaks compilation because custom inner attributes are unstable. The next thing I tried is to put the attribute on top of the mod my_mod; item, that doesn't work either, maybe because the macro is private to the module. Putting a plain #[rustfmt::skip] on top of the module item does work, but it skips the entire file when I only want to skip a specific invocation.

To illustrate, here is a patch against an empty Git repo you could apply with git am. cargo fmt does not skip the invocation:

From 592ea7bd5114c3208105aebe89929eb4978f2cf3 Mon Sep 17 00:00:00 2001
From: Alan Wu <[email protected]>
Date: Tue, 26 Apr 2022 14:25:37 -0400
Subject: [PATCH] Example project for rustfmt bug report

The `rustfmt::skip::macros` attribute does not seem to work.
---
 Cargo.toml     |  6 ++++++
 src/lib.rs     |  3 +++
 src/private.rs | 16 ++++++++++++++++
 3 files changed, 25 insertions(+)
 create mode 100644 Cargo.toml
 create mode 100644 src/lib.rs
 create mode 100644 src/private.rs

diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..c0b487c
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "rustfmt-skip-macro"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..d1a6314
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,3 @@
+// #[rustfmt::skip]
+#[rustfmt::skip::macros(items)]
+mod private;
diff --git a/src/private.rs b/src/private.rs
new file mode 100644
index 0000000..d484423
--- /dev/null
+++ b/src/private.rs
@@ -0,0 +1,16 @@
+// Custom inner attributes are unstable so I can't use them
+// as suggested by the README.
+// https://github.com/rust-lang/rust/issues/54726
+// #![rustfmt::skip::macros(items)]
+
+// A macro private to this module
+macro_rules! items {
+    ($($arg:item)*) => { $($arg)* };
+}
+
+// I would like rustfmt to skip this invocation
+items! (
+        const _: u8 = 0;
+
+
+);
-- 
2.32.0 (Apple Git-132)

Maybe the example in the tips section of the README should be updated to stop suggesting to use custom inner attributes, but I don't know what would be a better alternative.

I tried on rustfmt 1.4.38-stable (7737e0b5 2022-04-04) and rustfmt 1.4.38-nightly (18f314e7 2022-04-24).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions