]> Pileus Git - ~andy/linux/commitdiff
checkpatch: check for if's with unnecessary parentheses
authorJoe Perches <joe@perches.com>
Thu, 23 Jan 2014 23:54:47 +0000 (15:54 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 24 Jan 2014 00:36:58 +0000 (16:36 -0800)
If statements don't need multiple parentheses around tested comparisons
like "if ((foo == bar))".

An == comparison maybe a sign of an intended assignment, so emit a
slightly different message if so.

Signed-off-by: Joe Perches <joe@perches.com>
Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Andy Whitcroft <apw@canonical.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
scripts/checkpatch.pl

index 3e0b3f4d34203143d69b9c3c2ccf24dea9ac7278..57f10db4accde6a935e5ee37332a70df3bb54b33 100755 (executable)
@@ -3254,6 +3254,20 @@ sub process {
                        }
                }
 
+# if statements using unnecessary parentheses - ie: if ((foo == bar))
+               if ($^V && $^V ge 5.10.0 &&
+                   $line =~ /\bif\s*((?:\(\s*){2,})/) {
+                       my $openparens = $1;
+                       my $count = $openparens =~ tr@\(@\(@;
+                       my $msg = "";
+                       if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
+                               my $comp = $4;  #Not $1 because of $LvalOrFunc
+                               $msg = " - maybe == should be = ?" if ($comp eq "==");
+                               WARN("UNNECESSARY_PARENTHESES",
+                                    "Unnecessary parentheses$msg\n" . $herecurr);
+                       }
+               }
+
 # Return of what appears to be an errno should normally be -'ve
                if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
                        my $name = $1;