checkpatch: warn on Rust unwrap and expect calls
Subsystems (5)
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
@@ -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
@@ -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)
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()andexpect_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
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by Harish-CS <[email protected]>--- Documentation/dev-tools/checkpatch.rst | 4 ++++ scripts/checkpatch.pl | 9 +++++++++ 2 files changed, 13 insertions(+)Documentation/dev-tools/checkpatch.rst +4
-0@@ -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, whichscripts/checkpatch.pl +9
-0@@ -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/) {
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()andexpect_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
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by Harish-CS <[email protected]>---
Documentation/dev-tools/checkpatch.rst| 4 ++++ scripts/checkpatch.pl | 9 +++++++++2 files changed, 13 insertions(+)Documentation/dev-tools/checkpatch.rst +0
-0@@ -303,6 +303,10 @@ API usageSee: https://www.kernel.org/doc/html/latest/process/ deprecated.html#bug-and-bug-on + **RUST_PANIC_METHODS**
+ Rust methods that panic, such asunwrap()andexpect(), should be
+ avoided. Handle the error explicitly instead.
+
**CONSIDER_KSTRTO**
Thesimple_strtol(),simple_strtoll(),simple_strtoul(), andsimple_strtoull()functions explicitly ignore overflows, whichscripts/checkpatch.pl +9
-0@@ -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 filesif ($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
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
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()andexpect_err()so contributors notice them during patch review.
How about panic!()?