[PATCH] checkpatch: Allow --fix removal of unnecessary break statements

From: Joe Perches
Date: Sun Oct 18 2020 - 15:49:38 EST


switch/case use of break after a return or goto is unnecessary.

There is an existing warning for these uses, so add a --fix option too.

Signed-off-by: Joe Perches <joe@xxxxxxxxxxx>
---

For today's next, this would remove ~300 instances like:

case FOO:
return bar;
break;
or
case FOO:
goto bar;
break;

scripts/checkpatch.pl | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index fab38b493cef..22263b278e87 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3678,8 +3678,11 @@ sub process {
if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
my $tabs = $1;
if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
- WARN("UNNECESSARY_BREAK",
- "break is not useful after a goto or return\n" . $hereprev);
+ if (WARN("UNNECESSARY_BREAK",
+ "break is not useful after a goto or return\n" . $hereprev) &&
+ $fix) {
+ fix_delete_line($fixlinenr, $rawline);
+ }
}
}