public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Russ Allbery <rra@stanford.edu>
To: gcc@gcc.gnu.org
Subject: Re: Request for warnings on implicit bool to int conversions
Date: Wed, 28 Mar 2012 02:21:00 -0000	[thread overview]
Message-ID: <87398tea9n.fsf@windlord.stanford.edu> (raw)
In-Reply-To: <CAAiZkiA0OZwk2Rr6ygpXXjrhncrkv=ixFFb036Q0d7DGsRBJaw@mail.gmail.com>	(Gabriel Dos Reis's message of "Tue, 27 Mar 2012 20:18:53 -0500")

Gabriel Dos Reis <gdr@integrable-solutions.net> writes:

> I am trying to understand what the real issue is here.  Do you want
> -Wimplicit-char-to-int to?  -Wimplicit-short-to-int?  If not, why?
> where to stop?

I think it's more about conversion *to* bool than from bool, and it
catches places where code has been partly converted to bool (generally
because it predated C99) but the error conversion wasn't done properly.
For example, while returning true or false is common, returning 0 or -1 as
a boolean return value where 0 is success is also common (as with UNIX
library interfaces).  If someone messes up such a conversion and has a:

    status = -1;

somewhere, they get entirely the wrong result if status becomes a bool.
This warning would pick up cases like that.

The warnings about doing things like:

    bool b = true;
    b++;
    b += 3;

are somewhat akin to the warnings about doing arithmetic on void *
pointers -- code like that is possibly a sign that there's something
flawed with the algorithm and it should be rewritten to treat booleans as
actual booleans.  (For example, b++ could easily wrap, and unexpectedly
fast depending on the size of bool on a platform.)

-- 
Russ Allbery (rra@stanford.edu)             <http://www.eyrie.org/~eagle/>

  reply	other threads:[~2012-03-28  2:21 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 [this message]
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
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=87398tea9n.fsf@windlord.stanford.edu \
    --to=rra@stanford.edu \
    --cc=gcc@gcc.gnu.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 read-only IMAP folder(s) and NNTP newsgroup(s).