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: Thomas Wetmore <email@hidden>
- Date: Thu, 17 Oct 2013 20:28:39 -0400
As a long time Bell Labs engineer and a C purist, allow me to quote from the C specification from the K&R bible:
"if ( expression ) statement
"if ( expression ) statement else statement
"In both forms of the if statement, the expression, which must have ARITHMETIC or POINTER type [my emphasis], is evaluated, including all side-effects, and it if compares unequal to 0, the first substatement is executed. In the second form, the second substatement is executed if the expression is 0."
In C, and therefore in Objective C, there is no such thing as a "boolean expression". There are only expressions. If the expression evaluates to non-zero the "then" is executed. If it evaluates to 0 (and a nil pointer is DEFINED to have the value 0 in expressions) the "else", if there, is executed. Thinking that the expression must (or even should) have the form of some kind of logical expression is simply false and against the character of both C and Objective-C.
Some may think that explicitly checking a pointer for a nil-value is "good programming" practice, and if you do, go ahead and write it that way. But when you understand the semantics of the language, there is no need for such a form, and to some it looks odd. To me it belies a misunderstanding of standard C/Objective-C semantics.
Going a little further I do not like the fact that clang issues a warning for using an assignment type expression in an if expression; many good good C idioms use that technique to good effect. But since adding an extra pair of parentheses will silence the warning, I go along with it. I admit that that warning has found a couple bugs for me. However, adding a warning for if-expressions simply because they don't contain any logical operators strikes me as very wrong.
Tom Wetmore
CBW, DeadEnds Software
On Oct 17, 2013, at 1:58 PM, Rick Mann <email@hidden> wrote:
>
> On Oct 17, 2013, at 08:15 , Sean McBride <email@hidden> wrote:
>
>> I don't believe there is. But you can check for yourself by turning on -Weverything and seeing if any warning is emitted for your test case. I doubt it, because there would be so many false positives (where people are deliberately checking that a pointer is non-nil).
>
> That's a good idea.
>
> There's plenty of precedent for warnings that generate a lot of false positives because of conventional use. Take, for example, passing a floating point literal to a float parameter or assigning it to a float variable. You'll get a warning if you turn them all on.
>
> In my own code, I never test for nil without an explicit comparison, so I'd be okay with that.
>
> --
> Rick
>
>
>
> _______________________________________________
> 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
_______________________________________________
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