checkpatch: don't suggest else is not useful with chained
RFC PATCH 1/1 checkpatch: don't suggest else is not useful with chained
When if blocks are chained with "else if" like this:
if (a) c = 0; else if (b) break; else c = 1;
checkpatch recommends removing else:
WARNING: else is not generally useful after a break or return
Removing else will easily introduce logic errors in this situation so it's better to check if the preceding line has "else if" before issuing that warning.
---
I was only able to solve this case for single line if blocks but it would be useful to address also multi-line if blocks but it is beyond my understanding about checkpatch internals.
scripts/checkpatch.pl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
scripts/checkpatch.pl +8 -5
@@ -4037,14 +4037,17 @@ 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... +# but don't warn when there is "else if" on before that line to avoid logic errors 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 ($linenr >= 3 && + $lines[$linenr - 3] !~ /[} \t]else\s+if\s*\(/ && + ($prevline =~ /^\+\t{$tabs,$tabs}break\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 return\n" . $hereprev . "\n"); } }
Signature · 1 line
2.30.2
Joe Perches
maintainerscripts/checkpatch.pl
Quoted 29 lines · Ilpo Järvinen
When if blocks are chained with "else if" like this:
if (a) c = 0; else if (b) break; else c = 1;checkpatch recommends removing else:
WARNING: else is not generally useful after a break or return
Removing else will easily introduce logic errors in this situation so it's better to check if the preceding line has "else if" before issuing that warning.
Fixes 032a4c0f9a77 ("checkpatch: warn on unnecessary else after return or break")Signed-off-by Ilpo Järvinen <[email protected]>---
I was only able to solve this case for single line if blocks but it would be useful to address also multi-line if blocks but it is beyond my understanding about checkpatch internals.
scripts/checkpatch.pl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)scripts/checkpatch.pl +0
-0
[]
Quoted 10 lines
scripts/checkpatch.pl +1
-3@@ -4037,14 +4037,17 @@ 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... +# but don't warn when there is "else if" on before that line to avoid logic errors 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] &&
scripts/checkpatch.pl sub process { · line 4050
Quoted 10 lines
scripts/checkpatch.pl +7
-2- $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { + if ($linenr >= 3 && + $lines[$linenr - 3] !~ /[} \t]else\s+if\s*\(/ && + ($prevline =~ /^\+\t{$tabs,$tabs}break\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 return\n" . $hereprev . "\n");
I believe this wouldn't work on patches if the lines above are deletions.