[sheepdog] [PATCH v3] checkpatch.pl: forbid illegal empty lines after break;

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Tue Jan 22 10:05:16 CET 2013


From: Hitoshi Mitake <h.mitake at gmail.com>

This patch lets checkpatch.pl forbid illegal empty lines after break;.

Forbidden empty lines are categorized into 2 types:
1. empty lines after break; in a switch statement
2. empty lines after last break; in a compound statement

For example, a patch which contains diffs like this:
+void test(void)
+{
+	switch (x) {
+	case X:
+		break;		/* treated as an error */
+
+	case Y:
+		break;
+	default:
+		break;		/* treated as an error */
+
+	}
+
+	for ( ; ; ) {
+		if (0) {
+			break;	/* treated as an error */
+
+		}
+	}
+}
causes an error.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
v3
 * trivial fixes for the part of bzero() checking
 * a little bit cleaning
 * treat empty lines after last break; of a compound statement as an error
8<---

 script/checkpatch.pl |   36 +++++++++++++++++++++++++++++++++++-
 1 files changed, 35 insertions(+), 1 deletions(-)

diff --git a/script/checkpatch.pl b/script/checkpatch.pl
index 48917af..06aa26e 100755
--- a/script/checkpatch.pl
+++ b/script/checkpatch.pl
@@ -2841,8 +2841,42 @@ sub process {
 			}
 		}
 
+# forbid bzero()
 		if ($line =~ /\bbzero\(/) {
-			ERROR("BZERO", "bzero() is obsolete, use memset()" . $herecurr);
+			ERROR("BZERO", "bzero() is obsolete, use memset()\n" . $herecurr);
+		}
+
+# 1. forbid empty lines after break; of a switch statement
+# e.g.
+# +        break;
+# +
+# +    case XXX:
+# 
+# 2. forbid empty lines after break; of a compound statement
+# e.g.
+# +	for ( ; ; ) {
+# +		if (0) {
+# +			printf("asdf");
+# +			break;	/* treated as an error */
+# +
+# +		}
+# +	}
+		if ($line =~ /\bbreak;/) {
+		    my $ln = $linenr;
+		    while ($lines[$ln] =~ /^\+$/) { $ln++; }
+		    my $sline = $lines[$ln];
+
+		    if ($linenr < $ln) {
+			if ($sline =~ /\b(case|default)/) {
+			    ERROR("NL_AFTER_BREAK_IN_SWITCH",
+				  "don't insert empty lines after break; of a switch statement\n" . $herecurr);
+			}
+
+			if ($sline =~ /}/) {
+			    ERROR("NL_AFTER_LAST_BREAK",
+				  "don't insert empty lines after break; of a compound statement\n" . $herecurr);
+			}
+		    }
 		}
 	}
 
-- 
1.7.2.5




More information about the sheepdog mailing list