From: Michael Witten <mfwitten@gmail.com>
To: David Mathog <mathog@caltech.edu>
Cc: gcc@gcc.gnu.org
Subject: Re: Request for warnings on implicit bool to int conversions
Date: Wed, 28 Mar 2012 22:28:00 -0000 [thread overview]
Message-ID: <CAMOZ1BtNJcyP+cUdPaOgQE1iThQPcQgPpw=xHPawdNvRyoWXAA@mail.gmail.com> (raw)
In-Reply-To: <2528e1039cb3a3b7f83fdb41f312aa36@saf.bio.caltech.edu>
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
next prev parent reply other threads:[~2012-03-28 22:28 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-27 22:43 mathog
2012-03-28 1:00 ` Paolo Carlini
2012-03-28 1:19 ` Gabriel Dos Reis
2012-03-28 2:21 ` Russ Allbery
2012-03-28 2:30 ` Gabriel Dos Reis
2012-03-28 2:40 ` Russ Allbery
2012-03-28 2:48 ` Russ Allbery
2012-03-28 19:32 ` [bool wrapping] " Michael Witten
2012-03-28 20:30 ` mathog
2012-03-28 22:21 ` Michael Witten
2012-03-28 22:52 ` mathog
2012-03-29 10:29 ` David Brown
2012-03-29 17:49 ` mathog
2012-03-28 22:28 ` Michael Witten [this message]
2012-03-28 9:00 ` Franz Sirl
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='CAMOZ1BtNJcyP+cUdPaOgQE1iThQPcQgPpw=xHPawdNvRyoWXAA@mail.gmail.com' \
--to=mfwitten@gmail.com \
--cc=gcc@gcc.gnu.org \
--cc=mathog@caltech.edu \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).