scripts: checkpatch.pl add warning for dummy label
PATCH 1/1 scripts: checkpatch.pl add warning for dummy label
Added new warning DUMMY_LABEL in checkpatch.pl. This warns if return statement is encountered just after goto label target like "out:". In such scenario it is best to call "return;" directly instead of "goto out;"
--- scripts/checkpatch.pl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
scripts/checkpatch.pl +8 -4
@@ -3798,10 +3798,14 @@ sub process { 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); + $lines[$linenr - 3] =~ /^[ +]/) { + if ($lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { + WARN("RETURN_VOID", + "void function return statements are not generally useful\n" . $hereprev); + } elsif ($lines[$linenr - 3] =~ /^[ +]\s*$Ident\s*:/) { + WARN("DUMMY_LABEL", + "labels doing nothing are not generally useful\n" . $hereprev); + } } # if statements using unnecessary parentheses - ie: if ((foo == bar))
Signature · 1 line
1.9.1
Joe Perches
maintainerQuoted 4 lines · Nitin Kuppelur
Added new warning
DUMMY_LABELin checkpatch.pl. This warns if return statement is encountered just after goto label target like "out:". In such scenario it is best to call "return;" directly instead of "goto out;"
I'm not sure this is desired/correct.
There are many functions written like:
return_type foo(...)
{
[some initialization...]
err = some_test(...);
if (err)
goto out;
[...]out:
[some cleanup...] return err; }
In many cases, a void function is a simple variant of a non-void function and many people like to see the same code "shape" in functions, just without the init and cleanup bits.
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
Quoted 19 lines
scripts/checkpatch.pl +8
-4@@ -3798,10 +3798,14 @@ sub process { 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); + $lines[$linenr - 3] =~ /^[ +]/) { + if ($lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { + WARN("RETURN_VOID", + "void function return statements are not generally useful\n" . $hereprev); + } elsif ($lines[$linenr - 3] =~ /^[ +]\s*$Ident\s*:/) { + WARN("DUMMY_LABEL", + "labels doing nothing are not generally useful\n" . $hereprev); + } } # if statements using unnecessary parentheses - ie: if ((foo == bar))
Hi Joe,
Thanks for feedback.
The idea of adding new warning came from the review comments from one of my PATCH, where it was suggested by few reviewers to remove such "Do-Nothing" labels. But current checkpatch.pl does not warn about that.
[PATCH]
http://www.mail-archive.com/[email protected]/msg720639.html
Regards,
Nitin
Quoted 54 lines · Joe Perches
Quoted 4 lines · Nitin Kuppelur
Added new warning
DUMMY_LABELin checkpatch.pl. This warns if return statement is encountered just after goto label target like "out:". In such scenario it is best to call "return;" directly instead of "goto out;"I'm not sure this is desired/correct.
There are many functions written like:
return_typefoo(...){ [some initialization...] err = some_test(...); if (err) goto out; [...]out:
[some cleanup...] return err; }In many cases, a void function is a simple variant of a non-void function and many people like to see the same code "shape" in functions, just without the init and cleanup bits.
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0[]
Quoted 19 lines
scripts/checkpatch.pl +8
-4@@ -3798,10 +3798,14 @@ sub process { 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); + $lines[$linenr - 3] =~ /^[ +]/) { + if ($lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { + WARN("RETURN_VOID", + "void function return statements are not generally useful\n" . $hereprev); + } elsif ($lines[$linenr - 3] =~ /^[ +]\s*$Ident\s*:/) { + WARN("DUMMY_LABEL", + "labels doing nothing are not generally useful\n" . $hereprev); + } } # if statements using unnecessary parentheses - ie: if ((foo == bar))