Commit 840080a0 authored by Joe Perches's avatar Joe Perches Committed by Linus Torvalds

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: default avatarJoe Perches <joe@perches.com>
Reported-by: default avatarAl Viro <viro@ZenIV.linux.org.uk>
Cc: Elshad Mustafayev <elshadimo@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 66b47b4a
...@@ -2641,10 +2641,14 @@ sub process { ...@@ -2641,10 +2641,14 @@ sub process {
next if ($realfile !~ /\.(h|c)$/); next if ($realfile !~ /\.(h|c)$/);
# check indentation of any line with a bare else # check indentation of any line with a bare else
# (but not if it is a multiple line "if (foo) return bar; else return baz;")
# if the previous line is a break or return and is indented 1 tab more... # if the previous line is a break or return and is indented 1 tab more...
if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
my $tabs = length($1) + 1; my $tabs = length($1) + 1;
if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) { if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
defined $lines[$linenr] &&
$lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
WARN("UNNECESSARY_ELSE", WARN("UNNECESSARY_ELSE",
"else is not generally useful after a break or return\n" . $hereprev); "else is not generally useful after a break or return\n" . $hereprev);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment