mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH] checkpatch: Add warning for asymmetric brace use
       [not found]         ` <1326216615.26536.18.camel@joe2Laptop>
@ 2012-01-11 19:58           ` Joe Perches
  2012-01-11 20:43             ` Joe Perches
  0 siblings, 1 reply; 2+ messages in thread
From: Joe Perches @ 2012-01-11 19:58 UTC (permalink / raw)
  To: Andy Whitcroft, Dan Carpenter
  Cc: Julian Andres Klode, devel, Greg Kroah-Hartman, Andrew Morton,
	linux-kernel

On Tue, 2012-01-10 at 09:30 -0800, Joe Perches wrote:
> 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;
> }
> 
> 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(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index e3bfcbe..19f228c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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);
+					}
 				}
 			}
 		}



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [RFC PATCH] checkpatch: Add warning for asymmetric brace use
  2012-01-11 19:58           ` [RFC PATCH] checkpatch: Add warning for asymmetric brace use Joe Perches
@ 2012-01-11 20:43             ` Joe Perches
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Perches @ 2012-01-11 20:43 UTC (permalink / raw)
  To: Andy Whitcroft
  Cc: Dan Carpenter, Julian Andres Klode, devel, Greg Kroah-Hartman,
	Andrew Morton, linux-kernel

On Wed, 2012-01-11 at 11:58 -0800, Joe Perches wrote:
> Here's a possible checkpatch patch.
> It seems to work on the test cases I've tried.
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> +				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) {

> +						WARN("BRACES",
> +						     "braces {} should be used on all arms of this statement\n" . $herectx);
> +					}




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-01-11 20:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1324918660-25708-1-git-send-email-jak@jak-linux.org>
     [not found] ` <1324918660-25708-5-git-send-email-jak@jak-linux.org>
     [not found]   ` <20120110113552.GJ3644@mwanda>
     [not found]     ` <20120110114835.GB6325@jak-linux.org>
     [not found]       ` <20120110121453.GC16258@mwanda>
     [not found]         ` <1326216615.26536.18.camel@joe2Laptop>
2012-01-11 19:58           ` [RFC PATCH] checkpatch: Add warning for asymmetric brace use Joe Perches
2012-01-11 20:43             ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®