checkpatch: add --strict "pointer comparison to NULL" test

It seems there are more and more uses of "if (!ptr)" in preference to "if
(ptr == NULL)" so add a --strict test to emit a message when using the
latter form.

This also finds (ptr != NULL).

Fix it too if desired.

Signed-off-by: Joe Perches <joe@perches.com>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Greg KH <gregkh@linuxfoundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Joe Perches 2014-12-10 15:52:02 -08:00 committed by Linus Torvalds
parent 90ad30e5b2
commit b75ac618df
1 changed files with 14 additions and 0 deletions

View File

@ -4522,6 +4522,20 @@ sub process {
"Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
} }
# check for pointer comparisons to NULL
if ($^V && $^V ge 5.10.0) {
while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
my $val = $1;
my $equal = "!";
$equal = "" if ($4 eq "!=");
if (CHK("COMPARISON_TO_NULL",
"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
$fix) {
$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
}
}
}
# check for bad placement of section $InitAttribute (e.g.: __initdata) # check for bad placement of section $InitAttribute (e.g.: __initdata)
if ($line =~ /(\b$InitAttribute\b)/) { if ($line =~ /(\b$InitAttribute\b)/) {
my $attr = $1; my $attr = $1;