Hi,
I can't find a way to make Clang _always_ trigger warnings about
assignment inside a condition. When I create a new project (Mac or
iOS) in Xcode 4.3.1 and compile it, the following code does not
trigger any warnings:
- (id)init
{
if (self = [super init]) {
// ...
}
return self;
}
In my current project (which has been created in Xcode 4.2), this code
triggers the proper warning "Using the result of an assignment as a
condition without parentheses".
The following code triggers the warning as well (which is expected):
NSUInteger uid = 10;
if (uid = 11) {
// ...
}
BUT, in the same project I don't get any warnings in the following case:
if (self.uid = 11) {
// ...
}
Is this behavior expected? Which flag should I use in order for the
compiler to catch this case too?
Thanks!
(I'm not on the list, please /cc)
I can't find a way to make Clang _always_ trigger warnings about
assignment inside a condition. When I create a new project (Mac or
iOS) in Xcode 4.3.1 and compile it, the following code does not
trigger any warnings:
- (id)init
{
if (self = [super init]) {
// ...
}
return self;
}
We wanted to enable -Wparentheses by default, but we got a lot of pushback
over this specific idiom (particularly since Apple puts out example code
with this verbatim), so we moved it to a subgroup -Widiomatic-parentheses
that is not enabled by default. If you specifically enable -Wparentheses
instead of relying on the default behavior, you should see warnings about
this.
BUT, in the same project I don't get any warnings in the following case:
if (self.uid = 11) {
// ...
}
Is this behavior expected? Which flag should I use in order for the
compiler to catch this case too?
This is an oversight caused by a recent refactor; please file a bug.
John.
I can't find a way to make Clang _always_ trigger warnings about
assignment inside a condition. When I create a new project (Mac or
iOS) in Xcode 4.3.1 and compile it, the following code does not
trigger any warnings:
- (id)init
{
if (self = [super init]) {
// ...
}
return self;
}
We wanted to enable -Wparentheses by default, but we got a lot of pushback
over this specific idiom (particularly since Apple puts out example code
with this verbatim), so we moved it to a subgroup -Widiomatic-parentheses
that is not enabled by default. If you specifically enable -Wparentheses
instead of relying on the default behavior, you should see warnings about
this.
BUT, in the same project I don't get any warnings in the following case:
if (self.uid = 11) {
// ...
}
Is this behavior expected? Which flag should I use in order for the
compiler to catch this case too?
This is an oversight caused by a recent refactor; please file a bug.
Do we issue a warning if self.uid is a c++object and '=' is overloaded?
- Fariborz
Good question. We should.
John.
AFAICT Apple's new preferred approach for self-assignment in initializers doesn't use assignment-in-if anymore. So maybe it should be reconsidered?
--Kyle Sluder
It wouldn’t be worth it. There is a ton of existing code, even if the coding practices have evolved. It’s also trivial to recognize, and the warning would add no value.
I haven't seen anything about a change to the standard init pattern. What is the new one?
Jon Shier