diff options
| author | Joe Perches <joe@perches.com> | 2014-01-23 18:54:47 -0500 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-01-23 19:36:58 -0500 |
| commit | 189248d8f4f3ac2fba30da9b40133b5891df95fc (patch) | |
| tree | 8c1f512c32463c29de4792b17a30822555cdbc76 /scripts | |
| parent | c76f4cb3d25e5dc84017d7e845072e9aef6037f4 (diff) | |
checkpatch: check for if's with unnecessary parentheses
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>
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/checkpatch.pl | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 3e0b3f4d3420..57f10db4accd 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
| @@ -3254,6 +3254,20 @@ sub process { | |||
| 3254 | } | 3254 | } |
| 3255 | } | 3255 | } |
| 3256 | 3256 | ||
| 3257 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) | ||
| 3258 | if ($^V && $^V ge 5.10.0 && | ||
| 3259 | $line =~ /\bif\s*((?:\(\s*){2,})/) { | ||
| 3260 | my $openparens = $1; | ||
| 3261 | my $count = $openparens =~ tr@\(@\(@; | ||
| 3262 | my $msg = ""; | ||
| 3263 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { | ||
| 3264 | my $comp = $4; #Not $1 because of $LvalOrFunc | ||
| 3265 | $msg = " - maybe == should be = ?" if ($comp eq "=="); | ||
| 3266 | WARN("UNNECESSARY_PARENTHESES", | ||
| 3267 | "Unnecessary parentheses$msg\n" . $herecurr); | ||
| 3268 | } | ||
| 3269 | } | ||
| 3270 | |||
| 3257 | # Return of what appears to be an errno should normally be -'ve | 3271 | # Return of what appears to be an errno should normally be -'ve |
| 3258 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { | 3272 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
| 3259 | my $name = $1; | 3273 | my $name = $1; |
