linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Warn on unnecessary void function return statements
@ 2014-06-02 16:58 Joe Perches
  2014-06-16 23:28 ` Anish Bhatt
  2014-06-18 17:44 ` [PATCH V2] " Joe Perches
  0 siblings, 2 replies; 13+ messages in thread
From: Joe Perches @ 2014-06-02 16:58 UTC (permalink / raw)
  To: Andrew Morton; +Cc: LKML

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

Signed-off-by: Joe Perches <joe@perches.com>
---
 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,})/) {



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-02 16:58 [PATCH] checkpatch: Warn on unnecessary void function return statements Joe Perches
@ 2014-06-16 23:28 ` Anish Bhatt
  2014-06-17  0:28   ` Joe Perches
  2014-06-18 17:44 ` [PATCH V2] " Joe Perches
  1 sibling, 1 reply; 13+ messages in thread
From: Anish Bhatt @ 2014-06-16 23:28 UTC (permalink / raw)
  To: linux-kernel; +Cc: joe, akpm, Anish Bhatt

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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-16 23:28 ` Anish Bhatt
@ 2014-06-17  0:28   ` Joe Perches
  2014-06-17  0:44     ` Anish Bhatt
  0 siblings, 1 reply; 13+ messages in thread
From: Joe Perches @ 2014-06-17  0:28 UTC (permalink / raw)
  To: Anish Bhatt; +Cc: linux-kernel, akpm

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:
	;
}



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-17  0:28   ` Joe Perches
@ 2014-06-17  0:44     ` Anish Bhatt
  2014-06-17  2:00       ` Joe Perches
  0 siblings, 1 reply; 13+ messages in thread
From: Anish Bhatt @ 2014-06-17  0:44 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel, akpm

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


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-17  0:44     ` Anish Bhatt
@ 2014-06-17  2:00       ` Joe Perches
  2014-06-17  3:16         ` Sachin Kamat
  2014-06-17 19:37         ` Anish Bhatt
  0 siblings, 2 replies; 13+ messages in thread
From: Joe Perches @ 2014-06-17  2:00 UTC (permalink / raw)
  To: Anish Bhatt; +Cc: linux-kernel, akpm

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?


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-17  2:00       ` Joe Perches
@ 2014-06-17  3:16         ` Sachin Kamat
  2014-06-17  3:25           ` Joe Perches
  2014-06-17 19:37         ` Anish Bhatt
  1 sibling, 1 reply; 13+ messages in thread
From: Sachin Kamat @ 2014-06-17  3:16 UTC (permalink / raw)
  To: Joe Perches; +Cc: Anish Bhatt, linux-kernel, akpm

On Tue, Jun 17, 2014 at 7:30 AM, Joe Perches <joe@perches.com> 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.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-17  3:16         ` Sachin Kamat
@ 2014-06-17  3:25           ` Joe Perches
  2014-06-17  3:35             ` Sachin Kamat
  0 siblings, 1 reply; 13+ messages in thread
From: Joe Perches @ 2014-06-17  3:25 UTC (permalink / raw)
  To: Sachin Kamat; +Cc: Anish Bhatt, linux-kernel, akpm

On Tue, 2014-06-17 at 08:46 +0530, Sachin Kamat wrote:
> On Tue, Jun 17, 2014 at 7:30 AM, Joe Perches <joe@perches.com> 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...]
}



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-17  3:25           ` Joe Perches
@ 2014-06-17  3:35             ` Sachin Kamat
  0 siblings, 0 replies; 13+ messages in thread
From: Sachin Kamat @ 2014-06-17  3:35 UTC (permalink / raw)
  To: Joe Perches; +Cc: Anish Bhatt, linux-kernel, akpm

On Tue, Jun 17, 2014 at 8:55 AM, Joe Perches <joe@perches.com> wrote:
> On Tue, 2014-06-17 at 08:46 +0530, Sachin Kamat wrote:
>> On Tue, Jun 17, 2014 at 7:30 AM, Joe Perches <joe@perches.com> 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.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] checkpatch: Warn on unnecessary void function return statements
  2014-06-17  2:00       ` Joe Perches
  2014-06-17  3:16         ` Sachin Kamat
@ 2014-06-17 19:37         ` Anish Bhatt
  1 sibling, 0 replies; 13+ messages in thread
From: Anish Bhatt @ 2014-06-17 19:37 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel, akpm

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


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH V2] checkpatch: Warn on unnecessary void function return statements
  2014-06-02 16:58 [PATCH] checkpatch: Warn on unnecessary void function return statements Joe Perches
  2014-06-16 23:28 ` Anish Bhatt
@ 2014-06-18 17:44 ` Joe Perches
  2014-06-18 19:59   ` Anish Bhatt
  2014-06-19 20:18   ` Andrew Morton
  1 sibling, 2 replies; 13+ messages in thread
From: Joe Perches @ 2014-06-18 17:44 UTC (permalink / raw)
  To: Andrew Morton; +Cc: LKML, Anish Bhatt

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@perches.com>
---

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



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] checkpatch: Warn on unnecessary void function return statements
  2014-06-18 17:44 ` [PATCH V2] " Joe Perches
@ 2014-06-18 19:59   ` Anish Bhatt
  2014-06-19 20:18   ` Andrew Morton
  1 sibling, 0 replies; 13+ messages in thread
From: Anish Bhatt @ 2014-06-18 19:59 UTC (permalink / raw)
  To: Joe Perches; +Cc: LKML, Andrew Morton

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 <joe@perches.com>
> ---
>
> 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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] checkpatch: Warn on unnecessary void function return statements
  2014-06-18 17:44 ` [PATCH V2] " Joe Perches
  2014-06-18 19:59   ` Anish Bhatt
@ 2014-06-19 20:18   ` Andrew Morton
  2014-06-19 20:28     ` Joe Perches
  1 sibling, 1 reply; 13+ messages in thread
From: Andrew Morton @ 2014-06-19 20:18 UTC (permalink / raw)
  To: Joe Perches; +Cc: LKML, Anish Bhatt

On Wed, 18 Jun 2014 10:44:44 -0700 Joe Perches <joe@perches.com> 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 <joe@perches.com>
> ---
> 
> 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 <joe@perches.com>
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 <joe@perches.com>
Tested-by: Anish Bhatt <anish@chelsio.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 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 &&
_


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] checkpatch: Warn on unnecessary void function return statements
  2014-06-19 20:18   ` Andrew Morton
@ 2014-06-19 20:28     ` Joe Perches
  0 siblings, 0 replies; 13+ messages in thread
From: Joe Perches @ 2014-06-19 20:28 UTC (permalink / raw)
  To: Andrew Morton; +Cc: LKML, Anish Bhatt

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

Swell, thanks.



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-06-19 20:28 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-02 16:58 [PATCH] checkpatch: Warn on unnecessary void function return statements Joe Perches
2014-06-16 23:28 ` Anish Bhatt
2014-06-17  0:28   ` Joe Perches
2014-06-17  0:44     ` Anish Bhatt
2014-06-17  2:00       ` Joe Perches
2014-06-17  3:16         ` Sachin Kamat
2014-06-17  3:25           ` Joe Perches
2014-06-17  3:35             ` Sachin Kamat
2014-06-17 19:37         ` Anish Bhatt
2014-06-18 17:44 ` [PATCH V2] " Joe Perches
2014-06-18 19:59   ` Anish Bhatt
2014-06-19 20:18   ` Andrew Morton
2014-06-19 20:28     ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).