From: Luben Tuikov <luben.tuikov@amd.com>
To: Joe Perches <joe@perches.com>, Andy Whitcroft <apw@canonical.com>,
LKML <linux-kernel@vger.kernel.org>
Subject: checkpatch.pl: WARNING: else is not generally useful after a break or return
Date: Fri, 17 Apr 2020 15:20:56 -0400 [thread overview]
Message-ID: <064ada88-9fa3-6f6d-967c-00d4e2d325ae@amd.com> (raw)
Hi,
I'm getting what seems to be a false positive in this case:
:32: WARNING: else is not generally useful after a break or return
#32: FILE: drivers/gpu/drm/amd/amdgpu/amdgpu_job.c:55:
+ return 0;
+ } else {
for the following code, at the bottom of a function:
if (amdgpu_device_should_recover_gpu(ring->adev)) {
amdgpu_device_gpu_recover(ring->adev, job);
return 0;
} else {
drm_sched_suspend_timeout(&ring->sched);
return 1;
}
}
Which seems to be coming from commit:
commit 032a4c0f9a77ce565355c6e191553e853ba66f09
Author: Joe Perches <joe@perches.com>
Date: Wed Aug 6 16:10:29 2014 -0700
checkpatch: warn on unnecessary else after return or break
Using an else following a break or return can unnecessarily indent code
blocks.
ie:
for (i = 0; i < 100; i++) {
int foo = bar();
if (foo < 1)
break;
else
usleep(1);
}
is generally better written as:
for (i = 0; i < 100; i++) {
int foo = bar();
if (foo < 1)
break;
usleep(1);
}
Warn when a bare else statement is preceded by a break or return
indented 1 tab more than the else.
Signed-off-by: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
While I agree with what the commit is trying to do,
it doesn't seem to apply to the if-else statement which I quoted
above. That is, the "else" is not "bare"--to use the lingo of
the commit.
I suggest that no warning is issued when the "else" is a compound
statement, as shown in my example at the top of this email.
It is only natural to write:
if (amdgpu_device_should_recover_gpu(ring->adev)) {
amdgpu_device_gpu_recover(ring->adev, job);
return 0;
} else {
drm_sched_suspend_timeout(&ring->sched);
return 1;
}
}
instead of,
if (amdgpu_device_should_recover_gpu(ring->adev)) {
amdgpu_device_gpu_recover(ring->adev, job);
return 0;
}
drm_sched_suspend_timeout(&ring->sched);
return 1;
}
Regards,
--
Luben
next reply other threads:[~2020-04-17 19:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-17 19:20 Luben Tuikov [this message]
2020-04-17 19:56 ` checkpatch.pl: WARNING: else is not generally useful after a break or return Joe Perches
2020-04-17 20:44 ` Luben Tuikov
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=064ada88-9fa3-6f6d-967c-00d4e2d325ae@amd.com \
--to=luben.tuikov@amd.com \
--cc=apw@canonical.com \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).