Skip to contentSkip to conversation

checkpatch: Warn on unnecessary void function return statements

PATCH Superseded Joe Perches 4 participants 13 messages Jun 2, 2014 – Jun 19, 2014 Checkpatch

lore
Share & tools
lore

Joe Perches

author maintainer
#0 lore raw

PATCH 1/1 checkpatch: Warn on unnecessary void function return statements

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

---
 scripts/checkpatch.pl | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/checkpatch.pl +7 -0
index 7774025..f9bb12c 100755
@@ -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,})/) {
#1 lore raw

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

author maintainer
#2 lore raw
Quoted 7 lines · 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

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:

	;
}
#3 lore raw

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

author maintainer
#4 lore raw
Quoted 20 lines · Anish Bhatt

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);
}
Quoted 5 lines

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?

#5 lore raw
Quoted 30 lines · Joe Perches
Quoted 20 lines · Anish Bhatt

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);
}
Quoted 5 lines

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.

Quoted 8 lines

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;'

Signature · 2 lines
Regards,
Sachin.
author maintainer
#6 lore raw
Quoted 34 lines · Sachin Kamat
Quoted 33 lines · Joe Perches
Quoted 20 lines · Anish Bhatt

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);
}
Quoted 5 lines

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:

Quoted 8 lines
Quoted 5 lines

[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...]
}
#7 lore raw
Quoted 39 lines · Joe Perches
Quoted 38 lines · Sachin Kamat
Quoted 37 lines · Joe Perches
Quoted 20 lines · Anish Bhatt

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);
}
Quoted 5 lines

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;
}
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

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.

Signature · 2 lines
Regards,
Sachin.
#8 lore raw
In reply to Joe Perches
Quoted 30 lines · Joe Perches
Quoted 20 lines · Anish Bhatt

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);
}
Quoted 5 lines

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.

Quoted 9 lines

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

author maintainer
#9 lore raw

PATCH v2 1/1 checkpatch: Warn on unnecessary void function return statements

In reply to Joe Perches

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

---

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(+)
scripts/checkpatch.pl +12 -0
index 862cc7a..b191c88 100755
@@ -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,})/) {
#10 lore raw
Quoted 54 lines · Joe Perches

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

---

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(+)
scripts/checkpatch.pl +5 -0
index 862cc7a..b191c88 100755
@@ -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*$/ &&

scripts/checkpatch.pl sub process { · line 3487

Quoted 10 lines
scripts/checkpatch.pl +7 -0
+		    $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.

Tested-by Anish Bhatt on v2 1/1
#11 lore raw
In reply to Joe Perches
Quoted 27 lines · 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:

Quoted 8 lines
	return;
}

---

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.

---

 scripts/checkpatch.pl |   15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-reduce-false-positives-when-checking-void-function-return-statements +10 -5
diff -puN scripts/checkpatch.pl~checkpatch-reduce-false-positives-when-checking-void-function-return-statements 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 &&

_

author maintainer
#12 lore raw

On Thu, 2014-06-19 at 13:18 -0700, Andrew Morton wrote:
[]

Quoted 1 line

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

Swell, thanks.