From: Joe Perches Date: Fri, 3 Jan 2014 03:10:18 +0000 (+1100) Subject: checkpatch: check for if's with unnecessary parentheses X-Git-Tag: next-20140106~2^2~83 X-Git-Url: https://git.karo-electronics.de/?a=commitdiff_plain;h=88047892d41d9be2b556849f3df87d9f7501c408;p=karo-tx-linux.git 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 Reviewed-by: Josh Triplett Cc: Manfred Spraul Cc: Andy Whitcroft Signed-off-by: Andrew Morton --- diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 4c86498353c1..7e48d131df25 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -3310,6 +3310,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;