From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752330AbbH0DFm (ORCPT ); Wed, 26 Aug 2015 23:05:42 -0400 Received: from smtprelay0207.hostedemail.com ([216.40.44.207]:42376 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751900AbbH0DFk (ORCPT ); Wed, 26 Aug 2015 23:05:40 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::,RULES_HIT:41:355:379:541:599:960:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2197:2199:2393:2559:2562:2828:2919:3138:3139:3140:3141:3142:3353:3622:3653:3865:3866:3867:3868:3871:3872:3873:4321:5007:6119:6261:7903:8957:10004:10400:10848:11232:11658:11914:12043:12291:12517:12519:12555:12740:13069:13311:13357:21063:21080:21221,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0 X-HE-Tag: tooth09_6b7708adf3d2d X-Filterd-Recvd-Size: 2741 Message-ID: <1440644737.11525.64.camel@perches.com> Subject: Re: [PATCH] checkpatch: add --strict "pointer comparison to NULL" test From: Joe Perches To: Viresh Kumar Cc: Andrew Morton , Dan Carpenter , Greg KH , LKML , Mike Holmes , nmorey@kalray.eu Date: Wed, 26 Aug 2015 20:05:37 -0700 In-Reply-To: References: <1415905054.4223.7.camel@perches.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.12.11-0ubuntu3 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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;