mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Whitcroft <apw@canonical.com>, linux-kernel@vger.kernel.org
Subject: [PATCH 09/12] checkpatch: Add a --strict test for comparison to true/false
Date: Mon, 10 Jun 2013 15:20:47 -0700	[thread overview]
Message-ID: <ba0caa4e2117b0d7773b3157911a005dfeb51950.1370902448.git.joe@perches.com> (raw)
In-Reply-To: <cover.1370902448.git.joe@perches.com>

Comparing to true or false is error prone.

Add tests for the various forms of (foo == true) && (false != bar)
that are only reported with --strict.

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c43be81..c2d223c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3588,6 +3588,33 @@ sub process {
 			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
 		}
 
+# check for comparisons against true and false
+		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
+			my $lead = $1;
+			my $arg = $2;
+			my $test = $3;
+			my $otype = $4;
+			my $trail = $5;
+			my $op = "!";
+
+			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
+
+			my $type = lc($otype);
+			if ($type =~ /^(?:true|false)$/) {
+				if (("$test" eq "==" && "$type" eq "true") ||
+				    ("$test" eq "!=" && "$type" eq "false")) {
+					$op = "";
+				}
+
+				CHK("BOOL_COMPARISON",
+				    "Using comparison to $otype is error prone\n" . $herecurr);
+
+## maybe suggesting a correct construct would better
+##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
+
+			}
+		}
+
 # check for semaphores initialized locked
 		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
 			WARN("CONSIDER_COMPLETION",
-- 
1.8.1.2.459.gbcd45b4.dirty


  parent reply	other threads:[~2013-06-10 22:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-10 22:20 [PATCH 00/12] checkpatch: collection of neatenings and new "--fix" Joe Perches
2013-06-10 22:20 ` [PATCH 01/12] checkpatch: Remove quote from CamelCase test Joe Perches
2013-06-10 22:20 ` [PATCH 02/12] checkpatch: Improve network block comment test and message Joe Perches
2013-06-10 22:20 ` [PATCH 03/12] checkpatch: Warn when networking block comment lines don't start with * Joe Perches
2013-06-10 22:20 ` [PATCH 04/12] checkpatch: Warn on comparisons to jiffies Joe Perches
2013-06-10 22:20 ` [PATCH 05/12] checkpatch: Warn on comparisons to get_jiffies_64() Joe Perches
2013-06-10 22:20 ` [PATCH 06/12] checkpatch: Reduce false positive rate of "complex macros" Joe Perches
2013-06-10 22:20 ` [PATCH 07/12] checkpatch: Add a placeholder to check blank lines before declarations Joe Perches
2013-06-10 22:20 ` [PATCH 08/12] checkpatch: Don't warn on blank lines before/after braces as often Joe Perches
2013-06-10 22:20 ` Joe Perches [this message]
2013-06-10 22:20 ` [PATCH 10/12] checkpatch: Improve "no space after cast" test Joe Perches
2013-06-10 22:20 ` [PATCH 11/12] checkpatch: Create an EXPERIMENTAL --fix option to correct patches Joe Perches
2013-06-10 22:20 ` [PATCH 12/12] checkpatch: Move test for space before semicolon after operator spacing Joe Perches

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=ba0caa4e2117b0d7773b3157911a005dfeb51950.1370902448.git.joe@perches.com \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=apw@canonical.com \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®