Re: [PATCH v2 1/5] rust: add warn_on_err macro
From: Alexandre Courbot
Date: Fri Apr 24 2026 - 23:05:00 EST
On Sat Apr 25, 2026 at 1:16 AM JST, Gary Guo wrote:
> On Fri Apr 24, 2026 at 3:35 PM BST, Alexandre Courbot wrote:
>> On Thu Apr 23, 2026 at 8:34 PM JST, Gary Guo wrote:
>>> On Tue Apr 21, 2026 at 7:16 AM BST, Alexandre Courbot wrote:
>>>> While we already have the `warn_on` macro, a common usage pattern in
>>>> Rust is to check whether a `Result` is an error. Add a helper macro that
>>>> allows this.
>>>>
>>>> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
>>>> ---
>>>> rust/kernel/bug.rs | 10 ++++++++++
>>>> 1 file changed, 10 insertions(+)
>>>>
>>>> diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
>>>> index ed943960f851..2fefc0aeef81 100644
>>>> --- a/rust/kernel/bug.rs
>>>> +++ b/rust/kernel/bug.rs
>>>> @@ -130,3 +130,13 @@ macro_rules! warn_on {
>>>> cond
>>>> }};
>>>> }
>>>> +
>>>> +/// Report a warning if `res` is an error and return it unmodified.
>>>> +#[macro_export]
>>>> +macro_rules! warn_on_err {
>>>> + ($res:expr) => {{
>>>> + let res = $res;
>>>> + let _ = $crate::warn_on!(res.is_err());
>>>> + res
>>>> + }};
>>>> +}
>>>
>>>
>>> I think `warn_on_err` could just be expressed as
>>>
>>> res.inspect_err(|_| warn_on!(true))
>>
>> This would turn the verbose error message into something like
>>
>> [true] module.rs
>>
>> I.e. the expression passed as argument won't be printed.
>>
>> That being said, my version has the exact same problem and I don't see
>> how we can fix this without splitting `warn_on` and making it more
>> complex.
>>
>> Since this was done as a small convenience, I think I will just remove
>> this from this patchset and revisit later as a separate series.
>
> Something like this?
>
> #[macro_export]
> macro_rules! warn {
> () => {{
> const WARN_ON_FLAGS: u32 = $crate::bug::bugflag_taint($crate::bindings::TAINT_WARN);
>
> $crate::warn_flags!(file!(), WARN_ON_FLAGS);
> }}
> ($msg:expr) => {{
> #[cfg(CONFIG_DEBUG_BUGVERBOSE_DETAILED)]
> const _COND_STR: &str = concat!("[", $msg, "] ", file!());
> #[cfg(not(CONFIG_DEBUG_BUGVERBOSE_DETAILED))]
> const _COND_STR: &str = file!();
>
> const WARN_ON_FLAGS: u32 = $crate::bug::bugflag_taint($crate::bindings::TAINT_WARN);
>
> $crate::warn_flags!(_COND_STR, WARN_ON_FLAGS);
> }};
> }
>
> #[macro_export]
> macro_rules! warn_on {
> ($cond:expr) => {{
> let cond = $cond;
> if cond {
> warn!(stringify!($cond));
> }
> cond
> }};
> }
>
> Then you can just use a `warn!()` with custom message.
Exactly. But this is getting more involved than I initially thought and
will likely require more review, for a marginal improvement on the Nova
unload code - so let's do that as a separate series (that will also
allow us to merge it through the Rust tree).