Skip to contentSkip to conversation

checkpatch: warn on Rust unwrap and expect calls

PATCH In review Harish-CS 4 participants 5 messages Jul 7 – Jul 8 2 messages not in the archive

Subsystems (5)
lore
Share & tools
lore

Harish-CS

author
#0 lore raw

PATCH 1/1 checkpatch: warn on Rust unwrap and expect calls

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.

---
 Documentation/dev-tools/checkpatch.rst | 4 ++++
 scripts/checkpatch.pl                  | 9 +++++++++
 2 files changed, 13 insertions(+)
Documentation/dev-tools/checkpatch.rst +4 -0
index 6139a08c34cd..afa9787c1b9f 100644
@@ -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
scripts/checkpatch.pl +9 -0
index 2b7a42bbdd94..5bdb065370ea 100755
@@ -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/) {
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

Hi Harish,

Quoted 4 lines · 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.

We've had something similar some month ago, already. Maybe you like to check that discussion:

https://lore.kernel.org/rust-for-linux/[email protected]/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/[email protected]/

Best regards

Dirk

Quoted 43 lines
---
  Documentation/dev-tools/checkpatch.rst | 4 ++++
  scripts/checkpatch.pl                  | 9 +++++++++
  2 files changed, 13 insertions(+)
Documentation/dev-tools/checkpatch.rst +4 -0
index 6139a08c34cd..afa9787c1b9f 100644
@@ -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
scripts/checkpatch.pl +9 -0
index 2b7a42bbdd94..5bdb065370ea 100755
@@ -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 lore raw
Quoted 11 lines · Dirk Behme

Hi Harish,

Quoted 4 lines · 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.

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

Quoted 53 lines

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

Quoted 42 lines

---
  Documentation/dev-tools/checkpatch.rst | 4 ++++ scripts/checkpatch.pl                  | 9 +++++++++

  2 files changed, 13 insertions(+)
Documentation/dev-tools/checkpatch.rst +0 -0
index 6139a08c34cd..afa9787c1b9f 100644
@@ -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

scripts/checkpatch.pl +9 -0
index 2b7a42bbdd94..5bdb065370ea 100755
@@ -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/) {

A message that is not in the archive (sent off-list or not yet received): CAGjpMsQgjBLTMt3PU8FHokCOBWc59YFagSjqcE2ZJSBGixLGOA@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAGjpMsTvHHbfaGXDTK+pK9j59wpAx1zitfgRDeC1NAnbt2K67A@mail.gmail.com

#5 lore raw
Quoted 3 lines · Harish CS

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

#6 lore raw
In reply to Harish-CS
Quoted 4 lines · On Tue, 7 Jul 2026 13:51:04 +0530 Harish-CS <[email protected]> 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!()?