From: FUJITA Tomonori <tomo@flapping.org>
To: aliceryhl@google.com, gary@garyguo.net, ojeda@kernel.org,
rostedt@goodmis.org
Cc: a.hindborg@kernel.org, acourbot@nvidia.com,
bjorn3_gh@protonmail.com, boqun@kernel.org, dakr@kernel.org,
daniel.almeida@collabora.com, lossin@kernel.org,
mathieu.desnoyers@efficios.com, mhiramat@kernel.org,
tamird@kernel.org, tmgross@umich.edu, work@onurozkan.dev,
linux-trace-kernel@vger.kernel.org,
rust-for-linux@vger.kernel.org,
FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: [PATCH v2] rust: add mark_used macro
Date: Wed, 9 Sep 2026 21:02:10 +0900 [thread overview]
Message-ID: <20260909120210.2488674-1-tomo@flapping.org> (raw)
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
Macros whose expansion depends on the kernel configuration end up
ignoring some of their arguments, resulting in unused warnings.
Add a macro for the `if false { _ = ...; }` idiom used to avoid them,
and convert the existing open-coded users in `warn_flags!` and
`declare_trace!`.
Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
v2:
- Drop the comment in `declare_trace!`. The macro name says the same thing.
- Add Gary's Reviewed-by.
- Rebase on rust-next.
v1: https://lore.kernel.org/rust-for-linux/20260810075144.1245761-1-tomo@flapping.org/
---
rust/kernel/bug.rs | 18 ++++--------------
rust/kernel/lib.rs | 12 ++++++++++++
rust/kernel/tracepoint.rs | 4 +---
3 files changed, 17 insertions(+), 17 deletions(-)
diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 3566f0234ca4..c1e4eb7d4a70 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -55,9 +55,7 @@ macro_rules! warn_flags {
($file:expr, $flags:expr) => {
const FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING | $flags;
- if false {
- _ = $file;
- }
+ $crate::mark_used!($file);
// SAFETY:
// - `flags` and `size` are all compile-time constants, preventing
@@ -83,9 +81,7 @@ macro_rules! warn_flags {
#[cfg(all(CONFIG_BUG, CONFIG_UML))]
macro_rules! warn_flags {
($file:expr, $flags:expr) => {
- if false {
- _ = $file;
- }
+ $crate::mark_used!($file);
// SAFETY: It is always safe to call `warn_slowpath_fmt()`
// with a valid null-terminated string.
@@ -106,10 +102,7 @@ macro_rules! warn_flags {
#[cfg(all(CONFIG_BUG, any(CONFIG_LOONGARCH, CONFIG_ARM)))]
macro_rules! warn_flags {
($file:expr, $flags:expr) => {
- if false {
- _ = $file;
- _ = $flags;
- }
+ $crate::mark_used!($file, $flags);
// SAFETY: It is always safe to call `WARN_ON()`.
unsafe { $crate::bindings::WARN_ON(true) }
@@ -121,10 +114,7 @@ macro_rules! warn_flags {
#[cfg(any(testlib, not(CONFIG_BUG)))]
macro_rules! warn_flags {
($file:expr, $flags:expr) => {
- if false {
- _ = $file;
- _ = $flags;
- }
+ $crate::mark_used!($file, $flags);
};
}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 4d5c96ddc49c..3b38e6b8196b 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -227,6 +227,18 @@ macro_rules! container_of {
#[doc(hidden)]
pub fn assert_same_type<T>(_: T, _: T) {}
+/// Marks the given expressions as used.
+///
+/// This avoids "unused" warnings in kernel configurations that do not otherwise use them.
+#[macro_export]
+macro_rules! mark_used {
+ ($($e:expr),* $(,)?) => {
+ if false {
+ $( _ = $e; )*
+ }
+ };
+}
+
/// Helper for `.rs.S` files.
#[doc(hidden)]
#[macro_export]
diff --git a/rust/kernel/tracepoint.rs b/rust/kernel/tracepoint.rs
index c6e80aa99e8e..29d2d2d0aca8 100644
--- a/rust/kernel/tracepoint.rs
+++ b/rust/kernel/tracepoint.rs
@@ -38,9 +38,7 @@ macro_rules! declare_trace {
#[cfg(not(CONFIG_TRACEPOINTS))]
{
- // If tracepoints are disabled, insert a trivial use of each argument
- // to avoid unused argument warnings.
- $( let _unused = $argname; )*
+ $crate::mark_used!($($argname),*);
}
}
)*}
base-commit: 28924df2a08f440c73991b83028032c901de2ae4
--
2.43.0
reply other threads:[~2026-09-09 12:02 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260909120210.2488674-1-tomo@flapping.org \
--to=tomo@flapping.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=ojeda@kernel.org \
--cc=rostedt@goodmis.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.