Re: [PATCH v2] scripts: checkpatch: warn on Rust panicking methods

From: Joe Perches

Date: Sun Feb 01 2026 - 14:37:57 EST


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@xxxxxxxxx/

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);
> + }
> + }