All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Dan Carpenter <dan.carpenter@oracle.com>,
	Greg KH <gregkh@linuxfoundation.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Mike Holmes <mike.holmes@linaro.org>,
	nmorey@kalray.eu
Subject: Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL" test
Date: Wed, 26 Aug 2015 20:05:37 -0700	[thread overview]
Message-ID: <1440644737.11525.64.camel@perches.com> (raw)
In-Reply-To: <CAOh2x=mpUY4MN2U065azTbY_cH1bO0j_=i4QApXtoC5J+zEc6A@mail.gmail.com>

On Thu, 2015-08-27 at 07:49 +0530, Viresh Kumar wrote:
> Few colleagues asked me why isn't checkpatch warning for (NULL == ptr)
> or (NULL != ptr) checks, as it warns for (ptr == NULL) and (ptr != NULL).
> 
> Did you miss it? or was it intentional ?

I didn't miss it.

NULL == foo is relatively unusual and not really worth the
bother.

And because most likely, "CONST test variable" checks like
	NULL != foo
and
	0 < bar

should probably be a separate test.

Something like:
---
 scripts/checkpatch.pl | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index e14dcdb..457ddef 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4231,6 +4231,29 @@ sub process {
 			}
 		}
 
+# comparisons with a constant on the left
+		if ($^V && $^V ge 5.10.0 &&
+		    $line =~ /\b($Constant|[A-Z_]+)\s*($Compare)\s*($LvalOrFunc)/) {
+			my $const = $1;
+			my $comp = $2;
+			my $to = $3;
+			my $newcomp = $comp;
+			if (WARN("CONSTANT_COMPARISON",
+				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
+			    $fix) {
+				if ($comp eq "<") {
+					$newcomp = ">=";
+				} elsif ($comp eq "<=") {
+					$newcomp = ">";
+				} elsif ($comp eq ">") {
+					$newcomp = "<=";
+				} elsif ($comp eq ">=") {
+					$newcomp = "<";
+				}
+				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
+			}
+		}
+
 # Return of what appears to be an errno should normally be negative
 		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
 			my $name = $1;



  reply	other threads:[~2015-08-27  3:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-13 18:57 [PATCH] checkpatch: add --strict "pointer comparison to NULL" test Joe Perches
2015-08-27  2:19 ` Viresh Kumar
2015-08-27  3:05   ` Joe Perches [this message]
2015-08-27  7:09     ` Nicolas Morey Chaisemartin
2015-08-27  7:22       ` Joe Perches
2015-08-27 17:33         ` [PATCH] checkpatch: add constant comparison on left side test 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=1440644737.11525.64.camel@perches.com \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=dan.carpenter@oracle.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mike.holmes@linaro.org \
    --cc=nmorey@kalray.eu \
    --cc=viresh.kumar@linaro.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.