Linux Documentation
 help / color / mirror / Atom feed
* [PATCH] checkpatch: warn on Rust unwrap and expect calls
@ 2026-07-07  8:21 Harish-CS
  2026-07-08  5:41 ` Dirk Behme
  2026-07-08  9:14 ` FUJITA Tomonori
  0 siblings, 2 replies; 5+ messages in thread
From: Harish-CS @ 2026-07-07  8:21 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches
  Cc: Dwaipayan Ray, Lukas Bulwahn, Jonathan Corbet, Shuah Khan,
	Miguel Ojeda, linux-kernel, workflows, linux-doc, rust-for-linux,
	Harish-CS

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.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1191
Signed-off-by: Harish-CS <harish.cs.ss24@gmail.com>
---
 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/) {
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: warn on Rust unwrap and expect calls
  2026-07-07  8:21 [PATCH] checkpatch: warn on Rust unwrap and expect calls Harish-CS
@ 2026-07-08  5:41 ` Dirk Behme
  2026-07-08  5:45   ` Dirk Behme
  2026-07-08  9:14 ` FUJITA Tomonori
  1 sibling, 1 reply; 5+ messages in thread
From: Dirk Behme @ 2026-07-08  5:41 UTC (permalink / raw)
  To: Harish-CS, Andy Whitcroft, Joe Perches
  Cc: Dwaipayan Ray, Lukas Bulwahn, Jonathan Corbet, Shuah Khan,
	Miguel Ojeda, linux-kernel, workflows, linux-doc, rust-for-linux

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-harish.cs.ss24@gmail.com/T/#u

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-shivamklr@cock.li/

Best regards

Dirk


> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1191
> Signed-off-by: Harish-CS <harish.cs.ss24@gmail.com>
> ---
>   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/) {


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: warn on Rust unwrap and expect calls
  2026-07-08  5:41 ` Dirk Behme
@ 2026-07-08  5:45   ` Dirk Behme
       [not found]     ` <CAGjpMsQgjBLTMt3PU8FHokCOBWc59YFagSjqcE2ZJSBGixLGOA@mail.gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Dirk Behme @ 2026-07-08  5:45 UTC (permalink / raw)
  To: Harish-CS, Andy Whitcroft, Joe Perches
  Cc: Dwaipayan Ray, Lukas Bulwahn, Jonathan Corbet, Shuah Khan,
	Miguel Ojeda, linux-kernel, workflows, linux-doc, rust-for-linux

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- 
> harish.cs.ss24@gmail.com/T/#u

Arg, sorry, wrong link :(

https://lore.kernel.org/rust-for-linux/20260201155718.1623802-1-jason.kei.hall@gmail.com/

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- 
> shivamklr@cock.li/
> 
> Best regards
> 
> Dirk
> 
> 
>> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
>> Link: https://github.com/Rust-for-Linux/linux/issues/1191
>> Signed-off-by: Harish-CS <harish.cs.ss24@gmail.com>
>> ---
>>   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/) {
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: warn on Rust unwrap and expect calls
  2026-07-07  8:21 [PATCH] checkpatch: warn on Rust unwrap and expect calls Harish-CS
  2026-07-08  5:41 ` Dirk Behme
@ 2026-07-08  9:14 ` FUJITA Tomonori
  1 sibling, 0 replies; 5+ messages in thread
From: FUJITA Tomonori @ 2026-07-08  9:14 UTC (permalink / raw)
  To: harish.cs.ss24
  Cc: apw, joe, dwaipayanray1, lukas.bulwahn, corbet, skhan, ojeda,
	linux-kernel, workflows, linux-doc, rust-for-linux

On Tue,  7 Jul 2026 13:51:04 +0530
Harish-CS <harish.cs.ss24@gmail.com> 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.

How about panic!()?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] checkpatch: warn on Rust unwrap and expect calls
       [not found]       ` <CAGjpMsTvHHbfaGXDTK+pK9j59wpAx1zitfgRDeC1NAnbt2K67A@mail.gmail.com>
@ 2026-07-08 13:02         ` Miguel Ojeda
  0 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2026-07-08 13:02 UTC (permalink / raw)
  To: Harish CS
  Cc: Dirk Behme, Andy Whitcroft, Joe Perches, Dwaipayan Ray,
	Lukas Bulwahn, Jonathan Corbet, Shuah Khan, Miguel Ojeda,
	linux-kernel, workflows, linux-doc, rust-for-linux

On Wed, Jul 8, 2026 at 2:38 PM Harish CS <harish.cs.ss24@gmail.com> wrote:
>
> Do you think using Clippy's existing semantic lints would be a better
> direction to solve this in the build time itself ? Or do you have another approach in mind?

The most recent context about this is that we talked with upstream
Clippy in RustWeek about this again, and, bandwidth-allowing, I think
they are open to such lints.

In fact, for `// PANIC`, there is an open PR:

  https://github.com/rust-lang/rust-clippy/pull/15969

Helping test and review that one for the kernel use case would be
welcome, I would imagine, since getting lints right is harder than it
looks!

I have more context in the live list at:

  https://github.com/Rust-for-Linux/linux/issues/349

I hope that helps & thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-08 13:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-07  8:21 [PATCH] checkpatch: warn on Rust unwrap and expect calls Harish-CS
2026-07-08  5:41 ` Dirk Behme
2026-07-08  5:45   ` Dirk Behme
     [not found]     ` <CAGjpMsQgjBLTMt3PU8FHokCOBWc59YFagSjqcE2ZJSBGixLGOA@mail.gmail.com>
     [not found]       ` <CAGjpMsTvHHbfaGXDTK+pK9j59wpAx1zitfgRDeC1NAnbt2K67A@mail.gmail.com>
2026-07-08 13:02         ` Miguel Ojeda
2026-07-08  9:14 ` FUJITA Tomonori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox