Skip to content

Commit

Permalink
checkpatch: add --strict "pointer comparison to NULL" test
Browse files Browse the repository at this point in the history
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 <[email protected]>
Cc: Dan Carpenter <[email protected]>
Cc: Greg KH <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
JoePerches authored and torvalds committed Dec 11, 2014
1 parent 90ad30e commit b75ac61
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4522,6 +4522,20 @@ sub process {
"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)
if ($line =~ /(\b$InitAttribute\b)/) {
my $attr = $1;
Expand Down

0 comments on commit b75ac61

Please sign in to comment.