Re: Compiler flagging = v. ==
Re: Compiler flagging = v. ==
- Subject: Re: Compiler flagging = v. ==
- From: GoochRules! <email@hidden>
- Date: Thu, 11 Mar 2004 09:41:29 -0700
On Mar 10, 2004, at 11:44 PM, j o a r wrote:
The warning is suggesting that you should use parentheses around the
*assignment*, inside the parentheses for the evaluation of the truth
value. Like this:
if ((err = 0)) err = 1;
This syntax makes a bit of sense. First evaluate the stuff inside the
inner parentheses, then try to interpret the result of that evaluation
as a truth value.
I agree with your analysis, but in this case this is not helpful
because the user wanted to be warned about mistyping '=' rather than
'==' within an if statement. In fact, the 'extra' parentheses further
hide the problem.
A common example from Cocoa+ObjC is this:
NSEnumerator *enumerator = [someArray objectEnumerator];
id obj;
if (obj = [enumerator nextObject]) {}
...that should be turned into:
if ((obj = [enumerator nextObject])) {}
...or even better (IMO):
if ((obj = [enumerator nextObject]) != nil) {}
better still: if(nil != (obj = [enumerator nextObject])){}
in fact, if one takes the practice of always putting the constant term
on the left side of the expression -Wparentheses is not needed for
"if(0 = x)" results in an outright error. I realize that this is not
always the case, which is why I change -Wmost to -Wall directly
following creating a project or target.
j o a r
On 2004-03-10, at 22.20, Randall Meadows wrote:
Well, I did see that, actually, but I'm not omitting parens, I'm
omitting an equal sign, so it didn't seem apropos. Although it is
true that I have "an assignment in a context where a truth value is
expected".
The error message is a little odd (given my perspective, not the
doc's) with the code:
if (err = 0) err = 1;
the message is "warning: suggest parentheses around assignment used as
truth value"; I have parens, so why is it suggesting I use parens?
[demime 0.98b removed an attachment of type
application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.