checkpatch: Warn on comparisons to true and false
Joe Perches
maintainerPATCH 1/1 checkpatch: Warn on comparisons to true and false
Comparisons of A to true and false are better written as A and !A.
Bleat a message on use.
--- scripts/checkpatch.pl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
scripts/checkpatch.pl +17 -0
@@ -3538,6 +3538,23 @@ sub process { "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); } +# check for comparisons against true and false + if ($line =~ /\+\s*(.*?)($Lval)\s*(==|\!=)\s*(true|false)\b(.*)$/i) { + my $lead = $1; + my $arg = $2; + my $test = $3; + my $otype = $4; + my $trail = $5; + my $type = lc($otype); + my $op = "!"; + if (("$test" eq "==" && "$type" eq "true") || + ("$test" eq "!=" && "$type" eq "false")) { + $op = ""; + } + WARN("BOOL_COMPARISON", + "Using comparison to $otype is poor style. Use '${lead}${op}${arg}${trail}'\n" . $herecurr); + } + # check for semaphores initialized locked if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { WARN("CONSIDER_COMPLETION",
Quoted 23 lines · Joe Perches
Comparisons of A to true and false are better written as A and !A.
Bleat a message on use.
Signed-off-by Joe Perches <[email protected]>--- scripts/checkpatch.pl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)scripts/checkpatch.pl +5
-0@@ -3538,6 +3538,23 @@ sub process { "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); } +# check for comparisons against true and false + if ($line =~ /\+\s*(.*?)($Lval)\s*(==|\!=)\s*(true|false)\b(.*)$/i) { + my $lead = $1; + my $arg = $2; + my $test = $3;
scripts/checkpatch.pl sub process { · line 3555
Quoted 10 lines
scripts/checkpatch.pl +10
-0+ my $otype = $4; + my $trail = $5; + my $type = lc($otype); + my $op = "!"; + if (("$test" eq "==" && "$type" eq "true") || + ("$test" eq "!=" && "$type" eq "false")) { + $op = ""; + } + WARN("BOOL_COMPARISON", + "Using comparison to $otype is poor style. Use '${lead}${op}${arg}${trail}'\n" . $herecurr);
In a complex case such as a + b == false will this do the right thing? Not that I am sure that adding bools makes sense but assuming there is some valid complex lval.
-apw
Joe Perches
maintainerQuoted 3 lines · Andy Whitcroft
Quoted 2 lines · Joe Perches
Comparisons of A to true and false are better written as A and !A.
[]
Quoted 1 line
In a complex case such as a + b == false will this do the right thing?
Very sensible question. No it won't.
checkpatch doesn't understand expressions very well nor does it understand precedence operations between expressions and tests.
I did run it against the kernel tree and there weren't any I noticed like that though.
Quoted 2 lines
Not that I am sure that adding bools makes sense but assuming there is some valid complex lval.
$Lval in this case is a single variable and is neither a function nor an expression.
It doesn't match on:
if (func(x, y) == true) or if ((x | y) == true)
because of the ) before the ==
It will falsely match on expressions like:
if (x + y == true)
but as far as I can tell there aren't any uses like that in the kernel tree.
$ git grep -E "(==|\!=)\s*(true|false)\b" | \ cut -f2- -d":" |grep -P "\b(\+|\-)\b"
nor are there any and/or bit operator.
When I tried adding a test for:
"$Constant == $Lval" instead of "$Lval == $Constant"
like
0 == foo instead of foo == 0
there were _way_ too many false positives of the $Expression sort that I didn't add that test.
cheers, Joe
Quoted 7 lines · Joe Perches
like
0 == foo instead of foo == 0there were _way_ too many false positives of
the $Expression sort that I didn't add that test.
Makes sense then as it is.
Thanks.
-apw
Quoted 4 lines · On Tue, 09 Apr 2013 20:17:14 -0700 Joe Perches <[email protected]> wrote:
Comparisons of A to true and false are better written as A and !A.
Bleat a message on use.
hm. I'm counting around 1,100 instances of "== true" and "== false".
That's a lot of people to shout at. Is it really worthwhile? "foo==true" is a bit of a waste of space but I can't say that I find it terribly offensive.
Joe Perches
maintainerQuoted 5 lines · Andrew Morton
Quoted 3 lines · On Tue, 09 Apr 2013 20:17:14 -0700 Joe Perches <[email protected]> wrote:
Comparisons of A to true and false are better written as A and !A.
Bleat a message on use.hm. I'm counting around 1,100 instances of "== true" and "== false".
And about all of them in are staging, where I think they really should be fixed.
$ find . -maxdepth 2 -type d \
while read file ; do \
echo "$(git grep -E '(==|\!=)\s*(true|false)' $file | wc -l) $file"; \
done | sort -rn | head -10
1375 .
1298 ./drivers
1055 ./drivers/staging
63 ./drivers/net
59 ./drivers/gpu
24 ./net
20 ./drivers/media
17 ./net/nfc
13 ./fs
11 ./drivers/usbQuoted 1 line
That's a lot of people to shout at.
Not really.
Quoted 1 line
Is it really worthwhile?
I think so.
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Tue, 09 Apr 2013 20:17:14 -0700 Joe Perches <[email protected]> wrote:
Comparisons of A to true and false are better written as A and !A.
Quoted 7 lines
Quoted 1 line
Bleat a message on use.
hm. I'm counting around 1,100 instances of "== true" and "== false".
That's a lot of people to shout at. Is it really worthwhile? "foo==true" is a bit of a waste of space but I can't say that I find it terribly offensive.
It would be interesting to see how many people have historically screwed up and used (!a) when they mean (a) and vice versa, versus spelling it out longform. I'd be surprised if the results weren't skewed in favour of the more verbose form.
Dave
Joe Perches
maintainerQuoted 4 lines · Dave Jones
It would be interesting to see how many people have historically screwed up and used (!a) when they mean (a) and vice versa, versus spelling it out longform. I'd be surprised if the results weren't skewed in favour of the more verbose form.
I think it'd be interesting too though I'd
take the other side.
Quoted 10 lines · Dave Jones
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Tue, 09 Apr 2013 20:17:14 -0700 Joe Perches <[email protected]> wrote:
Comparisons of A to true and false are better written as A and !A.
Bleat a message on use.
hm. I'm counting around 1,100 instances of "== true" and "== false".
Quoted 8 lines
Quoted 3 lines
That's a lot of people to shout at. Is it really worthwhile? "foo==true" is a bit of a waste of space but I can't say that I find it terribly offensive.
It would be interesting to see how many people have historically screwed up and used (!a) when they mean (a) and vice versa, versus spelling it out longform. I'd be surprised if the results weren't skewed in favour of the more verbose form.
I see a the occasional reversed test in Smatch but normally these kind of bugs are detected with basic testing so they are rare.
regards,
dan carpenter
Joe Perches
maintainerQuoted 8 lines · Dan Carpenter
Quoted 4 lines · Dave Jones
It would be interesting to see how many people have historically screwed up and used (!a) when they mean (a) and vice versa, versus spelling it out longform. I'd be surprised if the results weren't skewed in favour of the more verbose form.
I see a the occasional reversed test in Smatch but normally these kind of bugs are detected with basic testing so they are rare.
I'd guess the most common error would be using
an int comparison when the value is not 0 or 1.
Non-zero is still "true" but isn't == true.
Quoted 10 lines · Dave Jones
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Tue, 09 Apr 2013 20:17:14 -0700 Joe Perches <[email protected]> wrote:
Comparisons of A to true and false are better written as A and !A.
Bleat a message on use.
hm. I'm counting around 1,100 instances of "== true" and "== false".
Quoted 8 lines
Quoted 3 lines
That's a lot of people to shout at. Is it really worthwhile? "foo==true" is a bit of a waste of space but I can't say that I find it terribly offensive.
It would be interesting to see how many people have historically screwed up and used (!a) when they mean (a) and vice versa, versus spelling it out longform. I'd be surprised if the results weren't skewed in favour of the more verbose form.
You have to consider that it is still possible to reverse the operator even if spelling it out, so you don't really gain anything:
bjorn@nemi:/usr/local/src/git/linux$ git grep -E '!=\s*(true|false)'|wc -l 63
and since most of these compare to true, they are also at risk wrt integers:
bjorn@nemi:/usr/local/src/git/linux$ git grep -E '!=\s*true'|wc -l 54
Based on a quick look at a few of these I guess they are mostly OK, testing against bool values. But I felt I had to share this little gem which showed up in drivers/gpu/drm/exynos/exynos_drm_vidi.c:
static int vidi_power_on(struct vidi_context *ctx, bool enable)
{
struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
struct device *dev = subdrv->dev;
DRM_DEBUG_KMS("%s\n", __FILE__);
if (enable != false && enable != true)
return -EINVAL;..
That's taking failsafe to the next step :)
Bjørn
Joe Perches
maintainerQuoted 12 lines · Bjørn Mork
I felt I had to share this little gem
which showed up indrivers/gpu/drm/exynos/exynos_drm_vidi.c:static int
vidi_power_on(struct vidi_context *ctx, bool enable){ struct exynos_drm_subdrv *subdrv = &ctx->subdrv; struct device *dev = subdrv->dev; DRM_DEBUG_KMS("%s\n", __FILE__); if (enable != false && enable != true) return -EINVAL;
Quoted 2 lines
..
That's taking failsafe to the next step :)
Cute. It's a relatively new driver too so I'd guess the "int -> bool conversion leftover" defense isn't too likely.