[PATCH V2] checkpatch: Warn on unnecessary void function return statements

From: Joe Perches
Date: Wed Jun 18 2014 - 13:44:53 EST


With some exceptions, warn on void functions that end with a
"return;", because it's unnecessary.

Check the closing brace at the start of a line.
If the line before that has a single tab, then return;
look at the line before that. If it's not a label,
emit a warning.

So, emit a warning on:

void function(...)
{
[...]
return;
}

but do not emit a warning on the below because
gcc requires any statement (including a bare
semicolon) before the closing function brace:

void function(...)
{
[...]
goto label;
[...]

label:
return;
}

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

V2: The previous patch had a few too many false positives
on styles that should be acceptable.

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

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 862cc7a..b191c88 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3470,6 +3470,18 @@ sub process {
}
}

+# unnecessary return in a void function
+# at end-of-function, with the previous line a single leading tab, then return;
+# and the line before that not a goto label target like "out:"
+ if ($sline =~ /^[ \+]}\s*$/ &&
+ $prevline =~ /^\+\treturn\s*;\s*$/ &&
+ $linenr >= 3 &&
+ $lines[$linenr - 3] =~ /^[ +]/ &&
+ $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
+ WARN("RETURN_VOID",
+ "void function return statements are not generally useful\n" . $hereprev);
+ }
+
# if statements using unnecessary parentheses - ie: if ((foo == bar))
if ($^V && $^V ge 5.10.0 &&
$line =~ /\bif\s*((?:\(\s*){2,})/) {


--
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/