Re: Any way to get a warning if a non-boolean type is used in an if expression?
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: "Clark S. Cox III" <email@hidden>
- Date: Wed, 16 Oct 2013 22:44:35 -0700
On Oct 16, 2013, at 21:44, Rick Mann <email@hidden> wrote:
>
> On Oct 16, 2013, at 21:16 , Keary Suska <email@hidden> wrote:
>
>> "if (self.active)" should never flag a warning, because it is not only perfectly legal but also not in any way an odd construct. The compiler can't know that you might not be testing for a nil value, for instance. Some advocate always having an explicit r-value, e.g. "if (self.active != nil)", which always makes the intention clear, but it is only a convention one could use to avoid ambiguity. In your case, this convention might have helped since implicit pointer conversion is usually flagged.
>
> I realize this, which is why I asked for a warning. clang is smart enough to recognize (I think) if something is actually its BOOL type, and I'd settle for this only working in Objective-C++. Maybe that's why it didn't work.
>
> I never use the perfectly legitimate but IMO lazy test for nil; I always make it explicit, and I accept this requirement when turning on the warning. Forcing that is much better than missing things like the one I missed.
Note that it is more complex than just looking for a boolean type. Remember that operators such as “<“, “==“ or “!” return int, and not _Bool, so something like this:
> if (self.active != nil)
is an if statement using a non-boolean type (because "self.active != nil” is an “int", not “_Bool”, “BOOL" or “bool").
Either way, I would recommend filing a bug asking for your warning.
--
Clark Smith Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden