rust: add mark_used macro
Subsystems (4)
PATCH 1/1 rust: add mark_used macro
From: FUJITA Tomonori <[email protected]>
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!.
--- rust/kernel/bug.rs | 18 ++++-------------- rust/kernel/lib.rs | 12 ++++++++++++ rust/kernel/tracepoint.rs | 5 ++--- 3 files changed, 18 insertions(+), 17 deletions(-)
rust/kernel/bug.rs +4 -14
@@ -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); }; }
rust/kernel/lib.rs +12 -0
@@ -266,6 +266,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]
rust/kernel/tracepoint.rs +2 -3
@@ -38,9 +38,8 @@ 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; )* +// If tracepoints are disabled, the arguments are unused. + $crate::mark_used!($($argname),*); } } )*}
base-commit: d8973c47544371613313ecb9a8c3e7b244aa9bbf
Signature · 1 line
2.43.0
Quoted 10 lines · FUJITA Tomonori
From: FUJITA Tomonori <[email protected]>
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 inwarn_flags!anddeclare_trace!.Signed-off-by FUJITA Tomonori <[email protected]>
Quoted 84 lines
--- rust/kernel/bug.rs | 18 ++++-------------- rust/kernel/lib.rs | 12 ++++++++++++ rust/kernel/tracepoint.rs | 5 ++--- 3 files changed, 18 insertions(+), 17 deletions(-)rust/kernel/bug.rs +4
-14@@ -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); }; }rust/kernel/lib.rs +12
-0@@ -266,6 +266,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] rust/kernel/tracepoint.rs +0
-0
rust/kernel/tracepoint.rs macro_rules! declare_trace { · line 41
Quoted 7 lines
rust/kernel/tracepoint.rs +1
-3@@ -38,9 +38,8 @@ 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; )* +// If tracepoints are disabled, the arguments are unused.
nit: the comment seems redundant now that the macro name is self-documenting.
Best,
Gary
Quoted 6 lines
rust/kernel/tracepoint.rs +1
-0+ $crate::mark_used!($($argname),*); } } )*}base-commit:
d8973c47544371613313ecb9a8c3e7b244aa9bbf