Skip to contentSkip to conversation

checkpatch: Check for space after "else" keyword

PATCH In review Michal Zylowski 2 participants 2 messages Aug 6, 2018 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 checkpatch: Check for space after "else" keyword

Current checkpatch implementation permits notation like:
} else{
in kernel code.
It looks like oversight and inconsistency in checkpatch rules (e.g. instruction like 'do' is tested).

Add regex for checking space after 'else' keyword and trigger error if space is not present.

---
 scripts/checkpatch.pl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
index 447857f..f7e135a 100755
@@ -4483,11 +4483,11 @@ sub process {
 
 #need space before brace following if, while, etc
 		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
-		    $line =~ /do\{/) {
+		    $line =~ /\b(?:else|do)\{/) {
 			if (ERROR("SPACING",
 				  "space required before the open brace '{'\n" . $herecurr) &&
 			    $fix) {
-				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
+				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
 			}
 		}
 
Signature · 1 line
2.7.4
maintainer Acked-by on v3 1/1
#1 lore raw
Quoted 10 lines · Michal Zylowski

Current checkpatch implementation permits notation like:
} else{
in kernel code.
It looks like oversight and inconsistency in checkpatch rules (e.g. instruction like 'do' is tested).

Add regex for checking space after 'else' keyword and trigger error if space is not present.

Quoted 22 lines
---
 scripts/checkpatch.pl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
index 447857f..f7e135a 100755
@@ -4483,11 +4483,11 @@ sub process {
 
 #need space before brace following if, while, etc
 		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
-		    $line =~ /do\{/) {
+		    $line =~ /\b(?:else|do)\{/) {
 			if (ERROR("SPACING",
 				  "space required before the open brace '{'\n" . $herecurr) &&
 			    $fix) {
-				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
+				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
 			}
 		}