History

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.

Joe Perches committed 12 years ago

Commit 189248d1 parent c76f4cb1 file · +14 −0Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+14−0
@@ -3254,6 +3254,20 @@ sub process { · 3253 unmodified lines
32543254 }
32553255 }
32563256
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+
32573271 # Return of what appears to be an errno should normally be -'ve
32583272 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
32593273 my $name = $1;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t