Re: [PATCH v2] rust: kunit: fix warning when !CONFIG_PRINTK

From: Alice Ryhl

Date: Tue Feb 24 2026 - 08:17:30 EST


On Tue, Feb 24, 2026 at 2:07 PM Alexandre Courbot <acourbot@xxxxxxxxxx> wrote:
>
> On Tue Feb 24, 2026 at 8:51 PM JST, Andreas Hindborg wrote:
> > "Alexandre Courbot" <acourbot@xxxxxxxxxx> writes:
> >
> >> If `CONFIG_PRINTK` is not set, then the following warnings are issued
> >> during build:
> >>
> >> warning: unused variable: `args`
> >> --> ../rust/kernel/kunit.rs:16:12
> >> |
> >> 16 | pub fn err(args: fmt::Arguments<'_>) {
> >> | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`
> >> |
> >> = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default
> >>
> >> warning: unused variable: `args`
> >> --> ../rust/kernel/kunit.rs:32:13
> >> |
> >> 32 | pub fn info(args: fmt::Arguments<'_>) {
> >> | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`
> >>
> >> Fix this by adding a no-op assignment using `args` when `CONFIG_PRINTK`
> >> is not set.
> >>
> >> Fixes: a66d733da801 ("rust: support running Rust documentation tests as KUnit ones")
> >> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
> >> ---
> >> Changes in v2:
> >> - Use a no-op assignment instead of the wider `#[allow(unused_variables)]`.
> >> - Link to v1: https://patch.msgid.link/20260224-unused_var_err-v1-1-c51d805eceb5@xxxxxxxxxx
> >> ---
> >> rust/kernel/kunit.rs | 8 ++++++++
> >> 1 file changed, 8 insertions(+)
> >>
> >> diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
> >> index f93f24a60bdd..a1edf7491579 100644
> >> --- a/rust/kernel/kunit.rs
> >> +++ b/rust/kernel/kunit.rs
> >> @@ -14,6 +14,10 @@
> >> /// Public but hidden since it should only be used from KUnit generated code.
> >> #[doc(hidden)]
> >> pub fn err(args: fmt::Arguments<'_>) {
> >> + // `args` is unused if `CONFIG_PRINTK` is not set - this avoids a build-time warning.
> >> + #[cfg(not(CONFIG_PRINTK))]
> >> + let _ = args;
> >
> > I think (didn't test) that you can use a conditional attribute [1] instead:
> >
> > pub fn err(
> > #[cfg_attr(not(CONFIG_PRINTK), expect(unused))]
> > args: fmt::Arguments<'_>
> > ) {
>
> Yup, that works as well, and I think I like it better as it is more
> localized. Alice, WDYT?

I think the other options is also quite localised. It's per function
argument too. shrug

Alice