[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <288d4aeb-af0d-4d50-bb0d-7a046abaaf10@de.bosch.com>
Date: Tue, 3 Feb 2026 07:21:34 +0100
From: Dirk Behme <dirk.behme@...bosch.com>
To: Jason Hall <jason.kei.hall@...il.com>, Joe Perches <joe@...ches.com>
CC: Miguel Ojeda <ojeda@...nel.org>, <rust-for-linux@...r.kernel.org>,
<linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v4] scripts: checkpatch: warn on Rust panicking methods
Hi Jason,
On 02/02/2026 14:56, Jason Hall wrote:
> Add regex check in checkpatch.pl for common Rust panicking methods
> like unwrap() and expect().
>
> Allow 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>
> ---
> Hi Dirk,
>
> Now using imperative mood. I decided to keep that logic stateless
> unless its agreed that we need to add state. Adding checks for #[test]
> and other test identifiers will make this much more complicated. There
> is already a check for // PANIC: that works fine.
Yes. I'm just slightly unclear what we do with existing code where using
`unwrap()` is fine/accepted/required? And with patches like
https://lore.kernel.org/rust-for-linux/20260131154016.270385-3-shivamklr@cock.li/
I would guess with this change we would get warnings on that?
So the idea is that we would need some add-on patches on top of this to
annotate existing code / patches with `// PANIC: ...` to stay warning clean?
Thanks,
Dirk
Btw, what's with Gary's comment to drop `expect()`?
> v4:
> - Use imperative mood in commit description.
> - Fix patch formatting and placement of '---' separator.
> v3:
> - Use non-capturing groups (?:) to optimize regex.
> v2:
> - Switch from \b to (\.|::) to avoid false positives in strings.
> - Add : 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:/ &&
Powered by blists - more mailing lists