[PATCH] Add fix case for open braces

From: Jeffrey Merkey
Date: Mon Feb 15 2016 - 20:20:23 EST


Some quite excellent fixes from Joe Perches that belong
back in the original. Submitting to Joe for inclusion. Joe
was the original author of these fixes that save a lot of time
rewriting code. Thanks Joe.

Signed-off-by: Jeffrey Merkey <jeffmerkey@xxxxxxxxx>
---
scripts/checkpatch.pl | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 874132b..71f93e8 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3108,9 +3108,15 @@ sub process {
#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";

if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
- ERROR("OPEN_BRACE",
- "that open brace { should be on the previous line\n" .
- "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+ if (ERROR("OPEN_BRACE",
+ "that open brace { should be on the previous line\n" .
+ "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+ $fix &&
+ $rawlines[$ctx_ln - 2] =~ /^\+/ &&
+ $rawlines[$ctx_ln - 1] =~ /^\+\s*{\s*$/) {
+ $fixed[$ctx_ln - 2] = rtrim($fixed[$ctx_ln - 2]) . ' {';
+ fix_delete_line($ctx_ln - 1, $rawlines[$ctx_ln - 1]);
+ }
}
if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
$ctx =~ /\)\s*\;\s*$/ &&
@@ -4807,8 +4813,15 @@ sub process {
$herectx .= raw_line($linenr, $n) . "\n";
}

- WARN("BRACES",
- "braces {} are not necessary for single statement blocks\n" . $herectx);
+ if (WARN("BRACES",
+ "braces {} are not necessary for single statement blocks\n" . $herectx) &&
+ $fix &&
+ $fixed[$fixlinenr] =~ /^\+.*\{\s*$/ &&
+ $fixed[$fixlinenr + 1] =~ /^\+.*;\s*$/ &&
+ $fixed[$fixlinenr + 2] =~ /^\+\s*\}\s*$/) {
+ $fixed[$fixlinenr] =~ s/\{\s*$//;
+ fix_delete_line($fixlinenr + 2, $rawlines[$fixlinenr + 2]);
+ }
}
}

--
1.8.3.1