mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>,
	Dwaipayan Ray <dwaipayanray1@gmail.com>,
	Lukas Bulwahn <lukas.bulwahn@gmail.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH v2 1/1] checkpatch: don't suggest else is not useful with chained
Date: Mon, 21 Mar 2022 17:30:44 +0200 (EET)	[thread overview]
Message-ID: <139ce92d-32f-4187-2f38-48e6ec354b74@linux.intel.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2007 bytes --]

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 <ilpo.jarvinen@linux.intel.com>
---

v2:
- Don't match spuriously to removed lines
- Extend check backwards to the first line with the same or lower indent 
  as "else" line

 scripts/checkpatch.pl | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b01c36a15d9d..ec0d9ac7e555 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4037,14 +4037,23 @@ 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" 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/)) {
-				WARN("UNNECESSARY_ELSE",
-				     "else is not generally useful after a break or return\n" . $hereprev);
+				my $ifline = $linenr - 3;
+				$tabs--;
+				while ($ifline >= 1 &&
+				       $lines[$ifline] !~ /^[^-]\t{0,$tabs}[^\t]/) {
+					$ifline--;
+				}
+				if ($lines[$ifline] !~ /^[\+ ]\t{$tabs,$tabs}(\} *|)else if\s*\(/) {
+					WARN("UNNECESSARY_ELSE",
+					     "else is not generally useful after a break or return\n" . $hereprev);
+				}
 			}
 		}
 
-- 
2.30.2

                 reply	other threads:[~2022-03-21 15:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=139ce92d-32f-4187-2f38-48e6ec354b74@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=apw@canonical.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®