[Linux-kernel-mentees] [PATCH] checkpatch: add fix option and improve msg for BOOL_COMPARISON

Aditya Srivastava yashsri421 at gmail.com
Wed Dec 9 10:58:33 UTC 2020

Currently, checkpatch warns if the user compares expression with boolean.

E.g., running checkpatch on commit 3b3a1a0b7824 ("staging: rtl8723bs:
hal: Modify comparison to constant in rtl8723bs_xmit.c") reports:

CHECK: Using comparison to true is error prone
+	if (ret == true)

Provide a fix by replacing the expression with preferred expression. i.e.,

1) If the check is for a 'true' or 'not false' expression, the operand
will get replaced with empty string.

2) Else, the operand gets replaced with '!', and the arguments follow.

Suggested-by: Joe Perches <joe at perches.com>
Signed-off-by: Aditya Srivastava <yashsri421 at gmail.com>
This fix is actually inspired from the comment mentioned in the script by Joe. I have added the Suggested-by line correspondingly.

 scripts/checkpatch.pl | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 8bd8e4518f7f..abd5a3d2e913 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6973,8 +6973,11 @@ sub process {
 					$op = "";
-				    "Using comparison to $otype is error prone\n" . $herecurr);
+					"Using comparison to '$otype' is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr) &&
+				    $fix) {
+					$fixed[$fixlinenr] =~ s/\b(?:true|false|$Lval)\s*(?:==|\!=)\s*(?:true|false|$Lval)\b/$op$arg/;
+				}
 ## maybe suggesting a correct construct would better
 ##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);

More information about the Linux-kernel-mentees mailing list