[PATCH] checkpatch: Warn on break after goto or return with same tab indentation

From: Joe Perches
Date: Tue Jul 08 2014 - 15:57:50 EST


Using break; after a goto or return is unnecessary so
emit a warning when the break is at the same indent level.

So this emits a warning on:

switch (foo) {
case 1:
goto err;
break;
}

but not on:

switch (foo) {
case 1:
if (bar())
goto err;
break;
}

Signed-off-by: Joe Perches <joe@xxxxxxxxxxx>
---
> AFAIK there's still no automatic detection of those cases.

There can be now...

scripts/checkpatch.pl | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 496f9ab..fc22f22 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2439,6 +2439,16 @@ sub process {
}
}

+# check indentation of a line with a break;
+# if the previous line is a goto or return and is indented the same # of tabs
+ 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);
+ }
+ }
+
# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
WARN("CONFIG_EXPERIMENTAL",


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/