public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Dirk Behme <dirk.behme@gmail.com>
To: Jason Hall <jason.kei.hall@gmail.com>,
	Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Joe Perches" <joe@perches.com>, "Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn.roy.baron@gmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakru@kernel.org>,
	"Dirk Behme" <dirk.behme@de.bosch.com>,
	"Andy Whitcroft" <apw@canonical.com>,
	"Dwaipayan Ray" <dwaipayanray1@gmail.com>,
	"Lukas Bulwahn" <lukas.bulwahn@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>
Subject: Re: [PATCH v9 2/2] scripts: checkpatch: add RUST_UNWRAP lint
Date: Sun, 8 Feb 2026 08:55:40 +0100	[thread overview]
Message-ID: <9ecadaa2-0f6b-411f-9faf-0ecc93b26bc2@gmail.com> (raw)
In-Reply-To: <20260207224907.234815-3-jason.kei.hall@gmail.com>

On 07.02.26 23:49, Jason Hall wrote:
> Warn against the use of .unwrap() and .expect() unless accompanied by
> a '// PANIC:' comment. This enforces safety standards in the Rust-
> for-Linux project until upstream Clippy lints are integrated.


I wonder if we could add some outcome from the mailing list discussion
to the commit message? E.g. what we consider to be false positives,
the handling of them and what we suppose to be fixed etc.


> 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>
> ---
>  scripts/rust_checkpatch.pl | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/scripts/rust_checkpatch.pl b/scripts/rust_checkpatch.pl
> index 56c1bc29d3f2..fa7adaed264c 100644
> --- a/scripts/rust_checkpatch.pl
> +++ b/scripts/rust_checkpatch.pl
> @@ -9,7 +9,21 @@ use warnings;
>  sub process_rust {
>      my ($line, $rawline, $herecurr) = @_;
>  
> -    # Reserve for future Rust-specific lints
> +    # Check for Rust unwrap/expect usage.
> +    # We skip lines that are already comments, assert macros (common in tests),
> +    # or have a '// PANIC:' justification.
> +    if ($line =~ /^\+/) {
> +        if ($line =~ /(?:\.|::)(?:unwrap|expect)\s*\(/ &&


Whats about the `.expect()` topic discussed in

https://lore.kernel.org/rust-for-linux/a798e6a368639f7a1ce633a6dfecd088d6ed4123.camel@perches.com/T/#m00723ad673727036e5fcf96a35f2f231ec9de31f

https://lore.kernel.org/rust-for-linux/a798e6a368639f7a1ce633a6dfecd088d6ed4123.camel@perches.com/T/#m5604274a633ef33eb474f95b54f797843d0fe1dd

?

> +            $rawline !~ /\/\/\s*PANIC:/ &&
> +            $line !~ /^\+\s*\/\// &&
> +            $line !~ /^\+\s*assert/) {
> +            return ("RUST_UNWRAP",
> +                    "unwrap() and expect() should generally be avoided in Rust kernel code.\n" .
> +                   "If the use is intended, please justify it with a '// PANIC:' comment.\n" .
> +                    "See: https://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust\n" .
> +                    $herecurr);
> +        }
> +    }
>      return ();
>  }

Just for the logs:

Running this on e.g.

https://lore.kernel.org/rust-for-linux/20260207-binder-shrink-vec-v3-v3-3-8ff388563427@cock.li/

gives


$ ./scripts/checkpatch.pl
0001-rust-alloc-add-KUnit-tests-for-Vec-shrink-operations.patch
WARNING: unwrap() and expect() should generally be avoided in Rust
kernel code.
If the use is intended, please justify it with a '// PANIC:' comment.
See:
https://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust
#52: FILE: rust/kernel/alloc/kvec.rs:1524:
+        let mut v: VVec<u32> = VVec::with_capacity(initial_capacity,
GFP_KERNEL).unwrap();

...

total: 0 errors, 21 warnings, 189 lines checked

(note: all 21 warnings are from `unwrap()`)

I'm not sure if it makes me happy to ignore these 21 warnings as false
positives ;)

Best regards

Dirk

  reply	other threads:[~2026-02-08  7:55 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-01 15:57 [PATCH] scripts: checkpatch: warn on Rust panicking methods Jason Hall
2026-02-01 17:19 ` Charalampos Mitrodimas
2026-02-01 18:30   ` [PATCH v2] " Jason Hall
2026-02-01 19:37     ` Joe Perches
2026-02-01 19:57   ` [PATCH v3] " Jason Hall
2026-02-02  5:38     ` Dirk Behme
2026-02-02 13:56       ` [PATCH v4] " Jason Hall
2026-02-03  6:21         ` Dirk Behme
2026-02-03 15:25           ` Jkhall81
2026-02-03 15:49             ` Onur Özkan
2026-02-03 16:02               ` Gary Guo
2026-02-03 16:32                 ` Onur Özkan
2026-02-03 16:54                   ` Gary Guo
2026-02-04 15:56                     ` Dirk Behme
2026-02-04 18:10                       ` Miguel Ojeda
2026-02-04 19:08                         ` Joe Perches
2026-02-05  1:42                           ` [PATCH v5] scripts: checkpatch: move Rust-specific lints to separate file Jason Hall
2026-02-05 20:55                             ` Miguel Ojeda
2026-02-06  8:31                               ` Dirk Behme
2026-02-06 17:41                                 ` Miguel Ojeda
2026-02-07 15:56                                   ` [PATCH v6] " Jason Hall
2026-02-07 16:07                                     ` Miguel Ojeda
2026-02-07 16:53                                       ` [PATCH v7] " Jason Hall
2026-02-07 18:46                                         ` Miguel Ojeda
2026-02-07 21:07                                           ` [PATCH v8 0/2] modularize Rust lints and add RUST_UNWRAP check Jason Hall
2026-02-07 21:07                                             ` [PATCH v8 1/2] scripts: checkpatch: move Rust-specific lints to separate file Jason Hall
2026-02-07 21:53                                               ` Miguel Ojeda
2026-02-07 22:49                                                 ` [PATCH v9 0/2] modularize Rust lints and add RUST_UNWRAP check Jason Hall
2026-02-07 22:49                                                   ` [PATCH v9 1/2] scripts: checkpatch: move Rust-specific lints to separate file Jason Hall
2026-02-07 22:49                                                   ` [PATCH v9 2/2] scripts: checkpatch: add RUST_UNWRAP lint Jason Hall
2026-02-08  7:55                                                     ` Dirk Behme [this message]
2026-02-08 14:01                                                       ` Jason Hall
2026-02-09  8:52                                                         ` Dirk Behme
2026-02-08  6:43                                                   ` [PATCH v9 0/2] modularize Rust lints and add RUST_UNWRAP check Greg KH
2026-02-14  6:11                                                   ` Dirk Behme
2026-02-14 23:30                                                     ` Miguel Ojeda
2026-02-14 23:32                                                       ` Miguel Ojeda
2026-02-07 21:07                                             ` [PATCH v8 2/2] scripts: checkpatch: add RUST_UNWRAP lint Jason Hall
2026-02-05 13:23                         ` [PATCH v4] scripts: checkpatch: warn on Rust panicking methods Dirk Behme
2026-02-05 21:00                           ` Miguel Ojeda
2026-02-04 18:11               ` Miguel Ojeda
2026-02-01 19:51 ` [PATCH] " Gary Guo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9ecadaa2-0f6b-411f-9faf-0ecc93b26bc2@gmail.com \
    --to=dirk.behme@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=apw@canonical.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn.roy.baron@gmail.com \
    --cc=boqun@kernel.org \
    --cc=dakru@kernel.org \
    --cc=dirk.behme@de.bosch.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=jason.kei.hall@gmail.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.com \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox