• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Any way to get a warning if a non-boolean type is used in an if expression?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any way to get a warning if a non-boolean type is used in an if expression?


  • Subject: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • From: Sean McBride <email@hidden>
  • Date: Fri, 18 Oct 2013 11:56:36 -0400
  • Organization: Rogue Research Inc.

On Fri, 18 Oct 2013 08:48:58 -0700, Jens Alfke said:

>> Relatedly, I think a lack of warning on this is even worse:
>>
>> BOOL x = ...
>> if (x == YES)
>>   ...
>
>Ouch, I’ve run into that before, especially with bit-tests like
>	BOOL x = flags & (1<<kFlagBit);
>If kFlagBit isn’t zero, the example you gave doesn’t work right. (A
>related even worse case is where kFlagBit > 7, in which case x always
>ends up as 0 because the flag gets truncated out of the 8-bit BOOL value
>entirely, so even a correctly-written ‘if’ statement fails.)
>
>My understanding is that with the ‘bool’ type my example would work
>correctly, because the assignment would do an implicit “!= 0”
>comparison, and maybe that was the justification for bool’s weird
>behavior. I’d much rather get a warning.

Yes, it would work with 'bool'.  Hopefully Obj-C will one day change BOOL from char to bool.

I filed this bug a few years ago: <http://llvm.org/PR9194>  Maybe I should file it in the black hole too...

One day I hope to have time to muck around in clang and try to add it. :)

Cheers,

--
____________________________________________________________
Sean McBride, B. Eng                 email@hidden
Rogue Research                        www.rogue-research.com
Mac Software Developer              Montréal, Québec, Canada

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Any way to get a warning if a non-boolean type is used in an if expression?
      • From: Scott Ribe <email@hidden>
    • Re: Any way to get a warning if a non-boolean type is used in an if expression?
      • From: David Duncan <email@hidden>
References: 
 >Re: Any way to get a warning if a non-boolean type is used in an if expression? (From: Sean McBride <email@hidden>)
 >Re: Any way to get a warning if a non-boolean type is used in an if expression? (From: Thomas Wetmore <email@hidden>)
 >Re: Any way to get a warning if a non-boolean type is used in an if expression? (From: Sean McBride <email@hidden>)
 >Re: Any way to get a warning if a non-boolean type is used in an if expression? (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Next by Date: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Previous by thread: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Next by thread: Re: Any way to get a warning if a non-boolean type is used in an if expression?
  • Index(es):
    • Date
    • Thread