Re: [PATCH] checkpatch: warn on Rust unwrap and expect calls

Dirk Behme <[email protected]> Wed, 8 Jul 2026 07:45:19 +0200
Newsgroups org.kernel.vger.workflows,org.kernel.vger.linux-doc,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On 08.07.2026 07:41, Dirk Behme wrote:
> Hi Harish,
> 
> On 07.07.2026 10:21, Harish-CS wrote:
>> Rust panic paths are discouraged in kernel code because panics currently
>> lead to BUG-like behavior. Add a checkpatch warning for newly added Rust
>> uses of unwrap(), unwrap_err(), expect() and expect_err() so contributors
>> notice them during patch review.
> 
> We've had something similar some month ago, already. Maybe you like to 
> check that discussion:
> 
> https://lore.kernel.org/rust-for-linux/20260707082104.90951-1- 
> [email protected]/T/#u

Arg, sorry, wrong link :(

https://lore.kernel.org/rust-for-linux/[email protected]/

Sorry

Dirk

> 
> If I remember correctly the main concern was how to filter out the 
> allowed/required usages. I think the example used that time was
> 
> https://lore.kernel.org/rust-for-linux/20260131154016.270385-3- 
> [email protected]/
> 
> Best regards
> 
> Dirk
> 
> 
>> Suggested-by: Miguel Ojeda <[email protected]>
>> Link: https://github.com/Rust-for-Linux/linux/issues/1191
>> Signed-off-by: Harish-CS <[email protected]>
>> ---
>>   Documentation/dev-tools/checkpatch.rst | 4 ++++
>>   scripts/checkpatch.pl                  | 9 +++++++++
>>   2 files changed, 13 insertions(+)
>>
>> diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/ 
>> dev-tools/checkpatch.rst
>> index 6139a08c34cd..afa9787c1b9f 100644
>> --- a/Documentation/dev-tools/checkpatch.rst
>> +++ b/Documentation/dev-tools/checkpatch.rst
>> @@ -303,6 +303,10 @@ API usage
>>       See: https://www.kernel.org/doc/html/latest/process/ 
>> deprecated.html#bug-and-bug-on
>> +  **RUST_PANIC_METHODS**
>> +    Rust methods that panic, such as unwrap() and expect(), should be
>> +    avoided.  Handle the error explicitly instead.
>> +
>>     **CONSIDER_KSTRTO**
>>       The simple_strtol(), simple_strtoll(), simple_strtoul(), and
>>       simple_strtoull() functions explicitly ignore overflows, which
>> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
>> index 2b7a42bbdd94..5bdb065370ea 100755
>> --- a/scripts/checkpatch.pl
>> +++ b/scripts/checkpatch.pl
>> @@ -3972,6 +3972,15 @@ sub process {
>>               }
>>           }
>> +# avoid Rust panicking methods
>> +        if ($realfile =~ /\.rs$/ &&
>> +            $line =~ /^\+.*\.(?:unwrap(?:_err)?| 
>> expect(?:_err)?)\s*\(/) {
>> +            my $msg_level = \&WARN;
>> +            $msg_level = \&CHK if ($file);
>> +            &{$msg_level}("RUST_PANIC_METHODS",
>> +                      "Avoid Rust panicking methods such as unwrap() 
>> and expect(); handle the error instead\n" . $herecurr);
>> +        }
>> +
>>   # check for .L prefix local symbols in .S files
>>           if ($realfile =~ /\.S$/ &&
>>               $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END) 
>> (?:_[A-Z_]+)?\s*\(\s*\.L/) {
>