2014-06-02 16:58:40

by Joe Perches

[permalink] [raw]
Subject: [PATCH] checkpatch: Warn on unnecessary void function return statements

void function lines that use a single tab then "return;"
are generally unnecessary.

Signed-off-by: Joe Perches <[email protected]>
---
scripts/checkpatch.pl | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7774025..f9bb12c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3470,6 +3470,13 @@ sub process {
}
}

+# unnecessary return in a void function? (a single leading tab, then return;)
+ if ($sline =~ /^\+\treturn\s*;\s*$/ &&
+ $prevline =~ /^\+/) {
+ WARN("RETURN_VOID",
+ "void function return statements are not generally useful\n" . $herecurr);
+ }
+
# if statements using unnecessary parentheses - ie: if ((foo == bar))
if ($^V && $^V ge 5.10.0 &&
$line =~ /\bif\s*((?:\(\s*){2,})/) {


2014-06-17 00:24:39

by Anish Bhatt

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

This seems to ignore the ability to exit from void return functions via a `return;` in case of an error or similar. Any attempt to bail out generates warnings with checkpathch.pl Perhaps it should check for returns only at the end of the function ? If not, is there a suggested way to do this ? goto labels can't be directly used in place either

-Anish Bhatt

2014-06-17 00:28:23

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

On Mon, 2014-06-16 at 16:28 -0700, Anish Bhatt wrote:
> This seems to ignore the ability to exit from void
> return functions via a `return;` in case of an error
> or similar. Any attempt to bail out generates warnings
> with checkpathch.pl Perhaps it should check for returns
> only at the end of the function ? If not, is there a
> suggested way to do this ? goto labels can't be directly
> used in place either

The only time checkpatch should bleat any message
is when there is a return; statement indented with
a single tab.

This form should be fine and not generate any
checkpatch warning.

void function(void)
{
...

if (err)
return;

...

}


If you want to use exit labels, I suggest you
use this form:

void function(void)
{

...

if (err)
goto exit;

...

exit:
;
}

2014-06-17 00:44:29

by Anish Bhatt

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

My code has multiple exit lables:
void function(void)
{
...

if (err1)
goto exit1;
...
if (err2)
goto exit2;

...
return; /* Good return, no errors */
exit1:
printk(err1);
return;
exit2:
printk(err2);
}

The single tabbed return was required to prevent the good return & err1
messages cascading down. The extra exit label with a noop looks weird,
but is passing checkpatch.pl --strict, so I will go with that, thanks.
-Anish

2014-06-17 02:00:33

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

On Mon, 2014-06-16 at 17:44 -0700, Anish Bhatt wrote:
> My code has multiple exit lables:
> void function(void)
> {
> ...
>
> if (err1)
> goto exit1;
> ...
> if (err2)
> goto exit2;
>
> ...
> return; /* Good return, no errors */
> exit1:
> printk(err1);
> return;
> exit2:
> printk(err2);
> }
>
> The single tabbed return was required to prevent the good return & err1
> messages cascading down. The extra exit label with a noop looks weird,
> but is passing checkpatch.pl --strict, so I will go with that, thanks.
> -Anish
>

Hmm, those return uses seem reasonable
to me.

Perhaps the test should warn only on
this specific 3 line sequence:

[any line but a label]
return;
}

Andrew? Anyone else? Opinions?

2014-06-17 03:16:41

by Sachin Kamat

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

On Tue, Jun 17, 2014 at 7:30 AM, Joe Perches <[email protected]> wrote:
> On Mon, 2014-06-16 at 17:44 -0700, Anish Bhatt wrote:
>> My code has multiple exit lables:
>> void function(void)
>> {
>> ...
>>
>> if (err1)
>> goto exit1;
>> ...
>> if (err2)
>> goto exit2;
>>
>> ...
>> return; /* Good return, no errors */
>> exit1:
>> printk(err1);
>> return;
>> exit2:
>> printk(err2);
>> }
>>
>> The single tabbed return was required to prevent the good return & err1
>> messages cascading down. The extra exit label with a noop looks weird,
>> but is passing checkpatch.pl --strict, so I will go with that, thanks.
>> -Anish
>>
>
> Hmm, those return uses seem reasonable
> to me.
>
> Perhaps the test should warn only on
> this specific 3 line sequence:
>
> [any line but a label]
> return;
> }
>
> Andrew? Anyone else? Opinions?

It should warn only if the return is followed by a value like
return 0; or return -EERROR_CODE; etc. and not just 'return;'


--
Regards,
Sachin.

2014-06-17 03:26:03

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

On Tue, 2014-06-17 at 08:46 +0530, Sachin Kamat wrote:
> On Tue, Jun 17, 2014 at 7:30 AM, Joe Perches <[email protected]> wrote:
> > On Mon, 2014-06-16 at 17:44 -0700, Anish Bhatt wrote:
> >> My code has multiple exit lables:
> >> void function(void)
> >> {
> >> ...
> >>
> >> if (err1)
> >> goto exit1;
> >> ...
> >> if (err2)
> >> goto exit2;
> >>
> >> ...
> >> return; /* Good return, no errors */
> >> exit1:
> >> printk(err1);
> >> return;
> >> exit2:
> >> printk(err2);
> >> }
> >>
> >> The single tabbed return was required to prevent the good return & err1
> >> messages cascading down. The extra exit label with a noop looks weird,
> >> but is passing checkpatch.pl --strict, so I will go with that, thanks.
> >> -Anish
> >>
> >
> > Hmm, those return uses seem reasonable
> > to me.
> >
> > Perhaps the test should warn only on
> > this specific 3 line sequence:
> >
> > [any line but a label]
> > return;
> > }
> >
> > Andrew? Anyone else? Opinions?
>
> It should warn only if the return is followed by a value like
> return 0; or return -EERROR_CODE; etc. and not just 'return;'

No. The compiler gets to warn on those.
checkpatch isn't a compiler.

It's a code style verifying and sometimes an
API misuse checking tool.

In this case, using return at the bottom of a
void function like

void function(void)
{
[code...]

return;
}

is undesired and would generally be written as

void function(void)
{
[code...]
}

2014-06-17 03:35:22

by Sachin Kamat

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

On Tue, Jun 17, 2014 at 8:55 AM, Joe Perches <[email protected]> wrote:
> On Tue, 2014-06-17 at 08:46 +0530, Sachin Kamat wrote:
>> On Tue, Jun 17, 2014 at 7:30 AM, Joe Perches <[email protected]> wrote:
>> > On Mon, 2014-06-16 at 17:44 -0700, Anish Bhatt wrote:
>> >> My code has multiple exit lables:
>> >> void function(void)
>> >> {
>> >> ...
>> >>
>> >> if (err1)
>> >> goto exit1;
>> >> ...
>> >> if (err2)
>> >> goto exit2;
>> >>
>> >> ...
>> >> return; /* Good return, no errors */
>> >> exit1:
>> >> printk(err1);
>> >> return;
>> >> exit2:
>> >> printk(err2);
>> >> }
>> >>
>> >> The single tabbed return was required to prevent the good return & err1
>> >> messages cascading down. The extra exit label with a noop looks weird,
>> >> but is passing checkpatch.pl --strict, so I will go with that, thanks.
>> >> -Anish
>> >>
>> >
>> > Hmm, those return uses seem reasonable
>> > to me.
>> >
>> > Perhaps the test should warn only on
>> > this specific 3 line sequence:
>> >
>> > [any line but a label]
>> > return;
>> > }
>> >
>> > Andrew? Anyone else? Opinions?
>>
>> It should warn only if the return is followed by a value like
>> return 0; or return -EERROR_CODE; etc. and not just 'return;'
>
> No. The compiler gets to warn on those.
> checkpatch isn't a compiler.

Right. I misunderstood the context of the discussion.
Sorry for the noise.

--
Regards,
Sachin.

2014-06-17 19:37:30

by Anish Bhatt

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Warn on unnecessary void function return statements

On 06/16/2014 07:00 PM, Joe Perches wrote:
> On Mon, 2014-06-16 at 17:44 -0700, Anish Bhatt wrote:
>> My code has multiple exit lables:
>> void function(void)
>> {
>> ...
>>
>> if (err1)
>> goto exit1;
>> ...
>> if (err2)
>> goto exit2;
>>
>> ...
>> return; /* Good return, no errors */
>> exit1:
>> printk(err1);
>> return;
>> exit2:
>> printk(err2);
>> }
>>
>> The single tabbed return was required to prevent the good return & err1
>> messages cascading down. The extra exit label with a noop looks weird,
>> but is passing checkpatch.pl --strict, so I will go with that, thanks.
>> -Anish
>>
>
> Hmm, those return uses seem reasonable
> to me.
>
> Perhaps the test should warn only on
> this specific 3 line sequence:
>
> [any line but a label]
> return;
> }
>
> Andrew? Anyone else? Opinions?
>
I think simply

return;
}

should trigger the warning. If you are using a label just to exit, you could just do it in-place (though possibly someone might want to a goto instead of multiple returns)

-Anish

2014-06-18 17:44:49

by Joe Perches

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

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 <[email protected]>
---

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,})/) {

2014-06-18 19:59:16

by Anish Bhatt

[permalink] [raw]
Subject: Re: [PATCH V2] checkpatch: Warn on unnecessary void function return statements

On Wed 18 Jun 2014 10:44:44 AM PDT, Joe Perches wrote:
> 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 <[email protected]>
> ---
>
> 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,})/) {
>
>

Confirming, no longer hitting previous false positives for me.
-Anish

--
As long as the music's loud enough, we won't hear the world falling
apart.

2014-06-19 20:18:40

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH V2] checkpatch: Warn on unnecessary void function return statements

On Wed, 18 Jun 2014 10:44:44 -0700 Joe Perches <[email protected]> wrote:

> 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 <[email protected]>
> ---
>
> V2: The previous patch had a few too many false positives
> on styles that should be acceptable.

Previous patch is now in mainline, so I did this:


From: Joe Perches <[email protected]>
Subject: checkpatch: reduce false positives when checking void function return statements

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

Signed-off-by: Joe Perches <[email protected]>
Tested-by: Anish Bhatt <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
---

scripts/checkpatch.pl | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)

diff -puN scripts/checkpatch.pl~checkpatch-reduce-false-positives-when-checking-void-function-return-statements scripts/checkpatch.pl
--- a/scripts/checkpatch.pl~checkpatch-reduce-false-positives-when-checking-void-function-return-statements
+++ a/scripts/checkpatch.pl
@@ -3484,12 +3484,17 @@ sub process {
}
}

-# unnecessary return in a void function? (a single leading tab, then return;)
- if ($sline =~ /^\+\treturn\s*;\s*$/ &&
- $prevline =~ /^\+/) {
+# 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" . $herecurr);
- }
+ "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 &&
_

2014-06-19 20:28:42

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH V2] checkpatch: Warn on unnecessary void function return statements

On Thu, 2014-06-19 at 13:18 -0700, Andrew Morton wrote:
[]
> Previous patch is now in mainline, so I did this:

Swell, thanks.