OSDN Git Service

checkpatch: add likely/unlikely comparison misuse test
authorJoe Perches <joe@perches.com>
Fri, 13 Feb 2015 22:38:38 +0000 (14:38 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 14 Feb 2015 05:21:39 +0000 (21:21 -0800)
Add a test for probably likely/unlikely misuses where the comparison is
likely misplaced

if (likely(foo) > 0)
vs
if (likely(foo > 0))

Signed-off-by: Joe Perches <joe@perches.com>
Cc: Christoph Jaeger <cj@linux.com>
Cc: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
scripts/checkpatch.pl

index 786017d..41223c9 100755 (executable)
@@ -5239,6 +5239,13 @@ sub process {
                              "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
                }
 
+# likely/unlikely comparisons similar to "(likely(foo) > 0)"
+               if ($^V && $^V ge 5.10.0 &&
+                   $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
+                       WARN("LIKELY_MISUSE",
+                            "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
+               }
+
 # whine mightly about in_atomic
                if ($line =~ /\bin_atomic\s*\(/) {
                        if ($realfile =~ m@^drivers/@) {