mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Elshad Mustafayev <elshadimo@gmail.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Al Viro <viro@ZenIV.linux.org.uk>,
	Andy Whitcroft <apw@canonical.com>
Subject: [PATCH] checkpatch: Add exception to return then else test
Date: Wed, 24 Sep 2014 22:03:54 -0700	[thread overview]
Message-ID: <1411621434.4026.9.camel@joe-AO725> (raw)
In-Reply-To: <1411619466.4026.2.camel@joe-AO725>

Add an exception to the return before else warning
when the line following it is also a return like:

	if (foo)
		return bar;
	else
		return baz;

This form of a test then return is at least as readable as

	if (foo)
		return bar;
	return baz;

so don't emit a warning on the first form.

Signed-off-by: Joe Perches <joe@perches.com>
Reported-by: Al Viro <viro@ZenIV.linux.org.uk>
---
> > Just what makes the replacement easier to follow?
> > 	if (foo)
> > 		return bar;
> > 	else
> > 		return baz;
> > is no less idiomatic than
> > 	if (foo)
> > 		return bar;
> > 	return baz;
> > 
> > Al, really annoyed by how the words "coding style issue" are getting used as
> > a magic incantation...
> 
> I agree.
> 
> I'll see about updating checkpatch to avoid warning on it

 scripts/checkpatch.pl | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 74bba23..52a223e 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2641,10 +2641,14 @@ sub process {
 		next if ($realfile !~ /\.(h|c)$/);
 
 # 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 ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
 			my $tabs = length($1) + 1;
-			if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
+			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);
 			}



      parent reply	other threads:[~2014-09-25  5:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <E1XX0bN-00032o-0x@feisty.vs19.net>
2014-09-25  4:31 ` [PATCH] kernel: acctc.c: Fixed else if not generally userful after a break or return warning Joe Perches
     [not found]   ` <CAAm9=9QeO_VtdY+fJ5JbrBhN52z=JUeU09Nh3Mp04QWf+Qyygw@mail.gmail.com>
2014-09-25  5:00     ` Al Viro
2014-09-25  5:05       ` Joe Perches
2014-09-25  5:03   ` Joe Perches [this message]

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=1411621434.4026.9.camel@joe-AO725 \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=apw@canonical.com \
    --cc=elshadimo@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@ZenIV.linux.org.uk \
    /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®