2021-10-01 20:50:24

by Utkarsh Verma

[permalink] [raw]
Subject: [PATCH v2] checkpatch: add check for continue statement in UNNECESSARY_ELSE

UNNECESSARY_ELSE only checks for the usage of else after a return or
break. But the same logic is also true for continue statement.

else used after a continue statement is unnecessary. So add a test
for continue statement also.

Signed-off-by: Utkarsh Verma <[email protected]>
---
scripts/checkpatch.pl | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c27d2312cfc3..edbb74dda5cb 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4011,15 +4011,15 @@ sub process {

# check indentation of any line with a bare else
# (but not if it is a multiple line "if (foo) return bar; else return baz;")
-# if the previous line is a break or return and is indented 1 tab more...
+# if the previous line is a break or continue or return and is indented 1 tab more...
if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
my $tabs = length($1) + 1;
- if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
+ if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|continue)\b/ ||
($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
defined $lines[$linenr] &&
$lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
WARN("UNNECESSARY_ELSE",
- "else is not generally useful after a break or return\n" . $hereprev);
+ "else is not generally useful after a break or continue or return\n" . $hereprev);
}
}

--
2.25.1


2021-10-01 21:13:32

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH v2] checkpatch: add check for continue statement in UNNECESSARY_ELSE

On Sat, 2021-10-02 at 02:17 +0530, Utkarsh Verma wrote:
> UNNECESSARY_ELSE only checks for the usage of else after a return or
> break. But the same logic is also true for continue statement.
>
> else used after a continue statement is unnecessary. So add a test
> for continue statement also.
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -4011,15 +4011,15 @@ sub process {
> ?
>
> ?# check indentation of any line with a bare else
> ?# (but not if it is a multiple line "if (foo) return bar; else return baz;")
> -# if the previous line is a break or return and is indented 1 tab more...
> +# if the previous line is a break or continue or return and is indented 1 tab more...
> ? if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
> ? my $tabs = length($1) + 1;
> - if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
> + if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|continue)\b/ ||
> ? ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
> ? defined $lines[$linenr] &&
> ? $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
> ? WARN("UNNECESSARY_ELSE",
> - "else is not generally useful after a break or return\n" . $hereprev);
> + "else is not generally useful after a break or continue or return\n" . $hereprev);
> ? }
> ? }
> ?
>

Maybe make the output specific to the break/continue/return
(untested)

if ($prevline =~ /^\+\t{$tabs,$tabs}(break|continue|return)\b/ &&
!($1 == "return" &&
defined $lines[$linenr] &&
$lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
WARN("UNNECESSARY_ELSE",
"else is not generally useful after a $1\n" . $hereprev);


2021-10-02 14:07:31

by Utkarsh Verma

[permalink] [raw]
Subject: [PATCH v3] checkpatch: add check for continue statement in UNNECESSARY_ELSE

UNNECESSARY_ELSE only checks for the usage of else after a return or
break. But the same logic is also true for continue statement.

else used after a continue statement is unnecessary. So add a test
for continue statement also.

Signed-off-by: Utkarsh Verma <[email protected]>
---
scripts/checkpatch.pl | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c27d2312cfc3..0eee086d87fe 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4011,15 +4011,15 @@ sub process {

# check indentation of any line with a bare else
# (but not if it is a multiple line "if (foo) return bar; else return baz;")
-# if the previous line is a break or return and is indented 1 tab more...
+# if the previous line is a break or continue or return and is indented 1 tab more...
if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
my $tabs = length($1) + 1;
- if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
- ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
- defined $lines[$linenr] &&
- $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
+ if ($prevline =~ /^\+\t{$tabs,$tabs}(break|continue|return)\b/ &&
+ !($1 eq "return" &&
+ defined $lines[$linenr] &&
+ $lines[$linenr] =~ /^[ \+]\t{$tabs,$tabs}return/)) {
WARN("UNNECESSARY_ELSE",
- "else is not generally useful after a break or return\n" . $hereprev);
+ "else is not generally useful after a $1\n" . $hereprev);
}
}

--
2.25.1

2021-10-03 05:14:52

by Lukas Bulwahn

[permalink] [raw]
Subject: Re: [PATCH v3] checkpatch: add check for continue statement in UNNECESSARY_ELSE

On Sat, Oct 2, 2021 at 4:03 PM Utkarsh Verma <[email protected]> wrote:
>
> UNNECESSARY_ELSE only checks for the usage of else after a return or
> break. But the same logic is also true for continue statement.

Just a bit nicer wording and improving your English writing:

But the same logic applies for the continue statement.

>
> else used after a continue statement is unnecessary. So add a test
> for continue statement also.
s/else/An else branch/

s/for continue statement/for the continue statement/
s/also/, too/

Other than that, all good. Great patch.

>
> Signed-off-by: Utkarsh Verma <[email protected]>
> ---
> scripts/checkpatch.pl | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index c27d2312cfc3..0eee086d87fe 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -4011,15 +4011,15 @@ sub process {
>
> # check indentation of any line with a bare else
> # (but not if it is a multiple line "if (foo) return bar; else return baz;")
> -# if the previous line is a break or return and is indented 1 tab more...
> +# if the previous line is a break or continue or return and is indented 1 tab more...
> if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
> my $tabs = length($1) + 1;
> - if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
> - ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
> - defined $lines[$linenr] &&
> - $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
> + if ($prevline =~ /^\+\t{$tabs,$tabs}(break|continue|return)\b/ &&
> + !($1 eq "return" &&
> + defined $lines[$linenr] &&
> + $lines[$linenr] =~ /^[ \+]\t{$tabs,$tabs}return/)) {
> WARN("UNNECESSARY_ELSE",
> - "else is not generally useful after a break or return\n" . $hereprev);
> + "else is not generally useful after a $1\n" . $hereprev);
> }
> }
>
> --
> 2.25.1
>