[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <875x8gqt3y.fsf@posteo.net>
Date: Sun, 01 Feb 2026 17:19:31 +0000
From: Charalampos Mitrodimas <charmitro@...teo.net>
To: Jason Hall <jason.kei.hall@...il.com>
Cc: apw@...onical.com, joe@...ches.com, ojeda@...nel.org,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] scripts: checkpatch: warn on Rust panicking methods
Jason Hall <jason.kei.hall@...il.com> writes:
> 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@...nel.org>
> Link: https://github.com/Rust-for-linux/linux/issues/1191
> Signed-off-by: Jason Hall <jason.kei.hall@...il.com>
> ---
> scripts/checkpatch.pl | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index c0250244cf7a..fa9f55031129 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 =~ /\b(unwrap|expect)\s*\(/ &&
Using `\.` instead of `\b` would reduce false positives on string
literas. E.g. `println!("use unwrap() here")` would not trigger a
warning.
> + $rawline !~ /\/\/\s*PANIC/ &&
Should this be:
$rawline != /\/\/\s*PANIC:/ &&
i.e. with the colon?
Cheers,
C. Mitrodimas
> + $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:/ &&
Powered by blists - more mailing lists