[vbox-dev] [PATCH 2/8] Fix an implicit conversion from pointer to bool
michal.necasek at oracle.com
Fri Mar 27 09:31:01 UTC 2015
So far I haven't seen a single argument that there's anything wrong
with 'if (ptr)'. As Michael pointed out, it is a very well established
C/C++ idiom. No other compiler we use considers it suspicious.
The same 'if (foo)' construct is widely used with integer types. Does
clang warn on that too? If not, why not? There's the same 'conversion to
bool' vs. 'comparison against zero' difference. Or perhaps
non-difference, because the value is converted to bool by internally
comparing it against zero.
I've been involved in compiler development enough that I know there is
a difficult balance to strike with warnings. On the one hand, warnings
can be extremely useful at flagging suspicious and potentially or
actually wrong code, on the other hand warnings which trigger on correct
code are not useful. If good code triggers too many warnings, the
inevitable result is that programmers turn off or ignore warnings.
Right now it sounds like clang is too eager and warns on perfectly
good code in this instance (however helpful its warnings are in other
On 3/26/2015 8:48 PM, Jung-uk Kim wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> On 03/26/2015 15:25, Michal Necasek wrote:
>> Sorry, I wasn't clear. By "what is the semantic difference" I meant
>> "under what circumstances will the conditional behave differently".
>> There must be some difference because otherwise what would be the
>> point of a warning?
> As I said, it does not behave differently under any circumstances. It
> just shuts up compiler warnings. I think the warning is to make sure
> its author is using a correct variable, i.e., "A variable of type foo
> is used where type bar should be used. Do you really want me to
> convert it to type bar?" or something like that.
> Some people say it's too noisy but I didn't write the compiler. :-P
>> Remember that programmers can have a hard time reading compilers'
>> minds, too.
> Last time I checked compilers lacked mind. ;-)
> Jung-uk Kim
>> ----- Original Message ----- From: jkim at FreeBSD.org To:
>> michal.necasek at oracle.com, vbox-dev at virtualbox.org Sent: Thursday,
>> March 26, 2015 7:59:29 PM GMT +01:00 Amsterdam / Berlin / Bern /
>> Rome / Stockholm / Vienna Subject: Re: [vbox-dev] [PATCH 2/8] Fix
>> an implicit conversion from pointer to bool
>> On 03/26/2015 14:03, Michal Necasek wrote:
>>> The question isn't whether a compiler can warn. The question is,
>>> why should it?
>> Although this specific code has absolutely no problem, compilers
>> are not smart enough to read authors' mind yet. ;-)
>>> What you quoted doesn't contradict what Frank said. There is a
>>> very well defined pointer to bool conversion. So what's the
>>> problem? Or in other words, what is the semantic difference
>> The difference is
>>> if (ptr)
>> ptr is converted to bool type first
>>> if (ptr != NULL)
>> ptr is compared to nullptr (if NULL is correctly defined as
>> Basically, the first is a conversion and the second is a
>> Jung-uk Kim
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2
> -----END PGP SIGNATURE-----
More information about the vbox-dev