[PATCH] scripts: checkpatch: add check for simplifying Result<()> in Rust
From: Manas via B4 Relay
Date: Thu Nov 28 2024 - 13:01:29 EST
From: Manas <manas18244@xxxxxxxxxxx>
The type Result<()> can be rewritten simply as Result, as default type
parameters are unit `()` and `Error` types already. Thus keep usage of
`Result` consistent throughout codebase.
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Link: https://github.com/Rust-for-Linux/linux/issues/1128
Not-yet-signed-off-by: Manas <manas18244@xxxxxxxxxxx>
---
Note: The blockage due to my Signed-off-by tag is being resolved in
private. For now I am adding Not-yet-signed-off-by tag.
---
Documentation/dev-tools/checkpatch.rst | 10 ++++++++++
scripts/checkpatch.pl | 9 +++++++++
2 files changed, 19 insertions(+)
diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/dev-tools/checkpatch.rst
index abb3ff6820766ee0c29112b256bcc44ce41fffba..7bf1fc7d4d7b490b32b8863365afe12a6732d6c1 100644
--- a/Documentation/dev-tools/checkpatch.rst
+++ b/Documentation/dev-tools/checkpatch.rst
@@ -1018,6 +1018,16 @@ Functions and Variables
return bar;
+ **SIMPLIFIED_RESULT**
+ simplifies Result<()> in Rust codebase as Result because default type
+ parameters are unit and Error types::
+
+ fn foo(x: Result<()>) -> Result<()> {}
+
+ can be rewritten as::
+
+ fn foo(x: Result) -> Result {}
+
Permissions
-----------
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9eed3683ad76caffbbb2418e5dbea7551d374406..ddc943fd4a62d58cee92f8723e7634af4ca69e32 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3574,6 +3574,15 @@ sub process {
}
}
+# Check for Result<()> usage in Rust code
+ if ($realfile =~ /\.rs$/ &&
+ $sline =~ /^\+.*Result<\(\)>/) {
+ $check = 1;
+ if (CHK("SIMPLIFIED_RESULT", "Result<()> can be rewritten simply as Result\n" . $herecurr) && $fix) {
+ $fixed[$fixlinenr] =~ s/Result<\(\)>/Result/g;
+ }
+ }
+
# ignore non-hunk lines and lines being removed
next if (!$hunk_line || $line =~ /^-/);
---
base-commit: 1dc707e647bc919834eff9636c8d00b78c782545
change-id: 20241125-add-checkpatch-result-simplify-8def44860489
Best regards,
--
Manas <manas18244@xxxxxxxxxxx>