linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Warn on comparisons to true and false
@ 2013-04-10  3:17 Joe Perches
  2013-04-10  9:33 ` Andy Whitcroft
  2013-04-10 22:57 ` Andrew Morton
  0 siblings, 2 replies; 12+ messages in thread
From: Joe Perches @ 2013-04-10  3:17 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Andy Whitcroft, LKML, Jacob Pan

Comparisons of A to true and false are better written
as A and !A.

Bleat a message on use.

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

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3fb6d86..080e7f6 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3538,6 +3538,23 @@ 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*(.*?)($Lval)\s*(==|\!=)\s*(true|false)\b(.*)$/i) {
+			my $lead = $1;
+			my $arg = $2;
+			my $test = $3;
+			my $otype = $4;
+			my $trail = $5;
+			my $type = lc($otype);
+			my $op = "!";
+			if (("$test" eq "==" && "$type" eq "true") ||
+			    ("$test" eq "!=" && "$type" eq "false")) {
+				$op = "";
+			}
+			WARN("BOOL_COMPARISON",
+			     "Using comparison to $otype is poor style. Use '${lead}${op}${arg}${trail}'\n" . $herecurr);
+		}
+
 # check for semaphores initialized locked
 		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
 			WARN("CONSIDER_COMPLETION",



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2013-04-11 14:25 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-10  3:17 [PATCH] checkpatch: Warn on comparisons to true and false Joe Perches
2013-04-10  9:33 ` Andy Whitcroft
2013-04-10 11:27   ` Joe Perches
2013-04-10 12:41     ` Andy Whitcroft
2013-04-10 22:57 ` Andrew Morton
2013-04-11  1:07   ` Joe Perches
2013-04-11  2:14   ` Dave Jones
2013-04-11  3:47     ` Joe Perches
2013-04-11  8:19     ` Dan Carpenter
2013-04-11  8:29       ` Joe Perches
2013-04-11 11:56     ` Bjørn Mork
2013-04-11 14:25       ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).