History

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.

Joe Perches committed 11 years ago

Commit b75ac611 parent 90ad30e1 file · +14 −0Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+14−0
@@ -4522,6 +4522,20 @@ sub process { · 4521 unmodified lines
45224522 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
45234523 }
45244524
4525+# check for pointer comparisons to NULL
4526+ if ($^V && $^V ge 5.10.0) {
4527+ while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4528+ my $val = $1;
4529+ my $equal = "!";
4530+ $equal = "" if ($4 eq "!=");
4531+ if (CHK("COMPARISON_TO_NULL",
4532+ "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4533+ $fix) {
4534+ $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4535+ }
4536+ }
4537+ }
4538+
45254539 # check for bad placement of section $InitAttribute (e.g.: __initdata)
45264540 if ($line =~ /(\b$InitAttribute\b)/) {
45274541 my $attr = $1;