* [RFC PATCH] checkpatch: add --strict "pointer comparison to NULL" test
@ 2014-11-05 1:45 Joe Perches
0 siblings, 0 replies; only message in thread
From: Joe Perches @ 2014-11-05 1:45 UTC (permalink / raw)
To: Andrew Morton; +Cc: Dan Carpenter, Greg KH, LKML
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.
---
Anyone think this is useful?
scripts/checkpatch.pl | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 24d6702..d4e08bc 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4490,6 +4490,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;
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2014-11-05 1:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-05 1:45 [RFC PATCH] checkpatch: add --strict "pointer comparison to NULL" test Joe Perches
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®