This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Request for warnings on implicit bool to int conversions


On Wed, Mar 28, 2012 at 20:30, David Mathog wrote:

> ...
>
>>  gcc -std=c99 -pedantic -Wall -O0 -fdump-tree-gimple d.c
>
>
> That's a trick worth knowing. Thanks!
> Ran that on a test program and in every case but one there was an implicit
>
>   (int) b
>
> on sections of code which I though should generate warnings.  (Math ops on
> bools, comparison operations of
> bool with ints.) The one exception in that little program was:
>
>  b=2;
>
> which came out as
>
>  b=1;
>
> In that case the compiler must have performed an implicit
>
>  1 = (bool) 2
>
> Also these forms all generated implicit int conversions
>
>  if(b==1)
>  if(b==2)
>  if(b==0)
>
> whereas
>
>  if(b)
>
> did not.
>
> So let's turn this around.  If the compiler warned on all implicit
> conversions of bool <-> int,
> what program lines would it warn on that it shouldn't?  It would warn on:
>
>  if(b==1)
>  if(b==0)
>
> but I would argue that line is inappropriate for a bool type, and should be
> written as:
>
>  if(b)
>
> With two bool variables, this also did not generate an implicit conversion
> (and so would not warn,
> which is appropriate):
>
>  if(b1 || b2)
>
> Whereas this less preferred form would have generated an implicit (int) [and
> a well deserved warning]
>
>  if(b1 + b2)


Your suggestion is probably a good idea, but as with all good ideas,
it will probably not get implemented because most people don't care.

Therefore, I'd suggest writing a patch and submitting it; people will
no doubt be a lot more receptive when you convey your thoughts in
hard-nosed, precise code, ESPECIALLY because that means the work has
already been done!

Sincerely,
Michael Witten


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]