[PATCH v3] scripts: checkpatch: warn on Rust panicking methods
From: Jason Hall
Date: Sun Feb 01 2026 - 15:01:48 EST
Added regex check in checkpatch.pl for common Rust panicking methods
like unwrap() and expect().
Allowed an exception if the line contains a '// PANIC:' comment.
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Link: https://github.com/Rust-for-linux/linux/issues/1191
Signed-off-by: Jason Hall <jason.kei.hall@xxxxxxxxx>
Hi, Joe,
I updated the regex, and I looked at Ryan Foster's patch. These two PANIC
checks don't seem to interfere with each other as far as I can see.
---
v3:
- Using non-capturing groups (?:) to optimize regex.
v2:
- Switched from \b to (\.|::) to avoid false positives in strings.
- Added : to PANIC comment check.
scripts/checkpatch.pl | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c0250244cf7a..37bdf602e7e7 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3834,6 +3834,17 @@ sub process {
# check we are in a valid source file if not then ignore this hunk
next if ($realfile !~ /\.(h|c|rs|s|S|sh|dtsi|dts)$/);
+# check for Rust unwrap/expect
+ if ($realfile =~ /\.rs$/ && $line =~ /^\+/) {
+ if ($line =~ /(?:\.|::)(?:unwrap|expect)\s*\(/ &&
+ $rawline !~ /\/\/\s*PANIC:/ &&
+ $line !~ /^\+\s*\/\// &&
+ $line !~ /^\+\s*assert/) {
+ WARN("RUST_UNWRAP",
+ "Avoid unwrap() or expect() in Rust code; use proper error handling (Result) or justify with a '// PANIC: ...' comment.\n" . $herecurr);
+ }
+ }
+
# check for using SPDX-License-Identifier on the wrong line number
if ($realline != $checklicenseline &&
$rawline =~ /\bSPDX-License-Identifier:/ &&
--
2.43.0