diff options
author | Joe Perches <joe@perches.com> | 2014-10-13 18:51:59 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-10-13 20:18:15 -0400 |
commit | 840080a08492bd2bb3314077b672b59c88bbe0e6 (patch) | |
tree | 7af7c1f6fbe2218941ec74d9d1eccebe681d10d8 /scripts | |
parent | 66b47b4a9dad00e45c049d79966de9a3a1f4d337 (diff) |
checkpatch: add exception to return then else test
Add an exception to the return before else warning when the line
following it is also a return like:
if (foo)
return bar;
else
return baz;
This form of a test then return is at least as readable as
if (foo)
return bar;
return baz;
so don't emit a warning on the first form.
Signed-off-by: Joe Perches <joe@perches.com>
Reported-by: Al Viro <viro@ZenIV.linux.org.uk>
Cc: Elshad Mustafayev <elshadimo@gmail.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 | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 74bba23a8df0..52a223ebcd10 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -2641,10 +2641,14 @@ sub process { | |||
2641 | next if ($realfile !~ /\.(h|c)$/); | 2641 | next if ($realfile !~ /\.(h|c)$/); |
2642 | 2642 | ||
2643 | # check indentation of any line with a bare else | 2643 | # check indentation of any line with a bare else |
2644 | # (but not if it is a multiple line "if (foo) return bar; else return baz;") | ||
2644 | # if the previous line is a break or return and is indented 1 tab more... | 2645 | # if the previous line is a break or return and is indented 1 tab more... |
2645 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { | 2646 | if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { |
2646 | my $tabs = length($1) + 1; | 2647 | my $tabs = length($1) + 1; |
2647 | if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) { | 2648 | if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || |
2649 | ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && | ||
2650 | defined $lines[$linenr] && | ||
2651 | $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { | ||
2648 | WARN("UNNECESSARY_ELSE", | 2652 | WARN("UNNECESSARY_ELSE", |
2649 | "else is not generally useful after a break or return\n" . $hereprev); | 2653 | "else is not generally useful after a break or return\n" . $hereprev); |
2650 | } | 2654 | } |