scripts/checkpatch.pl | 11 +++++++++++ 1 file changed, 11 insertions(+)
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@kernel.org>
Link: https://github.com/Rust-for-linux/linux/issues/1191
Signed-off-by: Jason Hall <jason.kei.hall@gmail.com>
---
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..43b4c0eebb96 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
On Sun, 2026-02-01 at 11:30 -0700, Jason Hall wrote:
> 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.
There's some desire to add rust comment checking.
https://lore.kernel.org/lkml/20260113211025.637889-1-foster.ryan.r@gmail.com/
Maybe this could be coordinated.
> diff --git 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*\(/ &&
Please use (?: to avoid capture groups
> + $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);
> + }
> + }
© 2016 - 2026 Red Hat, Inc.