[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20260205014218.2202385-1-jason.kei.hall@gmail.com>
Date: Wed, 4 Feb 2026 18:42:18 -0700
From: Jason Hall <jason.kei.hall@...il.com>
To: Joe Perches <joe@...ches.com>,
Miguel Ojeda <ojeda@...nel.org>
Cc: Dirk Behme <dirk.behme@...il.com>,
Gary Guo <gary@...yguo.net>,
Onur Özkan <work@...rozkan.dev>,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
Jason Hall <jason.kei.hall@...il.com>
Subject: [PATCH v5] scripts: checkpatch: move Rust-specific lints to separate file
Move Rust linting logic into scripts/rust_checkpatch.pl to prevent
further growth of the main checkpatch.pl script.
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.
Suggested-by: Joe Perches <joe@...ches.com>
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>
---
v5:
- Move Rust-specific logic to scripts/rust_checkpatch.pl (Suggested by Joe Perches).
- Add conditional loading hook in scripts/checkpatch.pl.
- Updated regex to use house-style comments.
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 | 14 ++++++++++++++
scripts/rust_checkpatch.pl | 25 +++++++++++++++++++++++++
2 files changed, 39 insertions(+)
create mode 100644 scripts/rust_checkpatch.pl
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c0250244cf7a..f75cb70ad0dd 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -20,6 +20,12 @@ my $D = dirname(abs_path($P));
my $V = '0.32';
+my $rust_checkpatch_available = 0;
+if (-e "$D/rust_checkpatch.pl") {
+ require "$D/rust_checkpatch.pl";
+ $rust_checkpatch_available = 1;
+}
+
use Getopt::Long qw(:config no_auto_abbrev);
my $quiet = 0;
@@ -2947,6 +2953,14 @@ sub process {
$cnt_lines++ if ($realcnt != 0);
+# Check for Rust-specific lints
+ if ($rust_checkpatch_available && $realfile =~ /\.rs$/) {
+ my ($type, $msg) = process_rust($line, $rawline, $herecurr);
+ if ($type) {
+ WARN($type, $msg);
+ }
+ }
+
# Verify the existence of a commit log if appropriate
# 2 is used because a $signature is counted in $commit_log_lines
if ($in_commit_log) {
diff --git a/scripts/rust_checkpatch.pl b/scripts/rust_checkpatch.pl
new file mode 100644
index 000000000000..69db5ded7371
--- /dev/null
+++ b/scripts/rust_checkpatch.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+# SPDX-License-Identifier: GPL-2.0
+#
+# (c) 2026, Jason K. Hall <jason.kei.hall@...il.com>
+
+use strict;
+use warnings;
+
+sub process_rust {
+ my ($line, $rawline, $herecurr) = @_;
+
+ # check for Rust unwrap/expect
+ if ($line =~ /^\+/) {
+ if ($line =~ /(?:\.|::)(?:unwrap|expect)\s*\(/ &&
+ $rawline !~ /\/\/\s*PANIC:/ &&
+ $line !~ /^\+\s*\/\// &&
+ $line !~ /^\+\s*assert/) {
+ return ("RUST_UNWRAP",
+ "Avoid unwrap() or expect() in Rust code; use proper error handling (Result) or justify with a '// PANIC: ...' comment.\n" . $herecurr);
+ }
+ }
+ return ();
+}
+
+1;
\ No newline at end of file
--
2.43.0
Powered by blists - more mailing lists