Skip to contentSkip to conversation

checkpatch: Add warning for asymmetric brace use

RFC PATCH In review Joe Perches 1 participant 2 messages Jan 11, 2012 6 messages not in the archive Checkpatch

lore
Share & tools
lore

Joe Perches

author maintainer
#6 lore raw

RFC PATCH 1/1 checkpatch: Add warning for asymmetric brace use

In reply to a message not in the archive
Quoted 13 lines · Joe Perches

Here's a snippet where checkpatch does not emit
a warning where it reasonably could:

int main(int argc, char **argv)

{
	int a;
	if (a) {
		int foo;
		int bar;
	} else
		int foo;
}
Quoted 1 line

Maybe you could look at it?

Hey Andy.
Here's a possible checkpatch patch.
It seems to work on the test cases I've tried.

---
 scripts/checkpatch.pl |   28 ++++++++++++++++++++--------
 1 files changed, 20 insertions(+), 8 deletions(-)
scripts/checkpatch.pl +20 -8
index e3bfcbe..19f228c 100755
@@ -2882,7 +2882,8 @@ sub process {
 			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
 			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
 			if ($#chunks > 0 && $level == 0) {
-				my $allowed = 0;
+				my @allowed = ();
+				my $allow = 0;
 				my $seen = 0;
 				my $herectx = $here . "\n";
 				my $ln = $linenr - 1;
@@ -2893,6 +2894,7 @@ sub process {
 					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
 					my $offset = statement_rawlines($whitespace) - 1;
 
+					$allowed[$allow] = 0;
 					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
 
 					# We have looked at and allowed this specific line.
@@ -2905,23 +2907,33 @@ sub process {
 
 					$seen++ if ($block =~ /^\s*{/);
 
-					#print "cond<$cond> block<$block> allowed<$allowed>\n";
+					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
 					if (statement_lines($cond) > 1) {
 						#print "APW: ALLOWED: cond<$cond>\n";
-						$allowed = 1;
+						$allowed[$allow] = 1;
 					}
 					if ($block =~/\b(?:if|for|while)\b/) {
 						#print "APW: ALLOWED: block<$block>\n";
-						$allowed = 1;
+						$allowed[$allow] = 1;
 					}
 					if (statement_block_size($block) > 1) {
 						#print "APW: ALLOWED: lines block<$block>\n";
-						$allowed = 1;
+						$allowed[$allow] = 1;
 					}
+					$allow++;
 				}
-				if ($seen && !$allowed) {
-					WARN("BRACES",
-					     "braces {} are not necessary for any arm of this statement\n" . $herectx);
+				if ($seen) {
+					my $sum_allowed = 0;
+					foreach (@allowed) {
+						$sum_allowed += $_;
+					}
+					if ($sum_allowed == 0) {
+						WARN("BRACES",
+						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
+					} elsif ($sum_allowed != $allow) {
+						WARN("BRACES",
+						     "braces {} should be used on all arms of this statement\n" . $herectx);
+					}
 				}
 			}
 		}
author maintainer
#7 lore raw
Quoted 2 lines · Joe Perches

Here's a possible checkpatch patch.
It seems to work on the test cases I've tried.

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 2933

Quoted 9 lines
scripts/checkpatch.pl +9 -0
+				if ($seen) {
+					my $sum_allowed = 0;
+					foreach (@allowed) {
+						$sum_allowed += $_;
+					}
+					if ($sum_allowed == 0) {
+						WARN("BRACES",
+						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
+					} elsif ($sum_allowed != $allow) {

I ran some more tests against the kernel source.
This works better as:

+ } elsif ($sum_allowed != $allow &&
+ $seen != $allow) {

Quoted 3 lines
scripts/checkpatch.pl +3 -0
+						WARN("BRACES",
+						     "braces {} should be used on all arms of this statement\n" . $herectx);
+					}