diff options
author | Joe Perches <joe@perches.com> | 2014-08-06 19:10:29 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-08-06 21:01:27 -0400 |
commit | 032a4c0f9a77ce565355c6e191553e853ba66f09 (patch) | |
tree | 472cf85c05fdcafe028e5b8140729327afcffeea /scripts | |
parent | ebfdc40969f24fc0cdd1349835d36e8ebae05374 (diff) |
checkpatch: warn on unnecessary else after return or break
Using an else following a break or return can unnecessarily indent code
blocks.
ie:
for (i = 0; i < 100; i++) {
int foo = bar();
if (foo < 1)
break;
else
usleep(1);
}
is generally better written as:
for (i = 0; i < 100; i++) {
int foo = bar();
if (foo < 1)
break;
usleep(1);
}
Warn when a bare else statement is preceded by a break or return
indented 1 tab more than the else.
Signed-off-by: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/checkpatch.pl | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index ac1d6b0a6f09..d833b737a295 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -2342,6 +2342,16 @@ sub process { | |||
2342 | # check we are in a valid C source file if not then ignore this hunk | 2342 | # check we are in a valid C source file if not then ignore this hunk |
2343 | next if ($realfile !~ /\.(h|c)$/); | 2343 | next if ($realfile !~ /\.(h|c)$/); |
2344 | 2344 | ||
2345 | # check indentation of any line with a bare else | ||
2346 | # if the previous line is a break or return and is indented 1 tab more... | ||
2347 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { | ||
2348 | my $tabs = length($1) + 1; | ||
2349 | if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) { | ||
2350 | WARN("UNNECESSARY_ELSE", | ||
2351 | "else is not generally useful after a break or return\n" . $hereprev); | ||
2352 | } | ||
2353 | } | ||
2354 | |||
2345 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). | 2355 | # discourage the addition of CONFIG_EXPERIMENTAL in #if(def). |
2346 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { | 2356 | if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { |
2347 | WARN("CONFIG_EXPERIMENTAL", | 2357 | WARN("CONFIG_EXPERIMENTAL", |