spurious clang warning
spurious clang warning
- Subject: spurious clang warning
- From: Jean-Denis MUYS <email@hidden>
- Date: Thu, 08 Dec 2011 08:53:42 +0000
- Thread-topic: spurious clang warning
My class Concrete1 is one of the subclasses of a common superclass Abstract. It overrides an Abstract class method that allocates an instance of some subclass of Abstract, usually a Concrete1, but not always. As far as I understand, this is the core idea behind a class cluster.
Anyway, clang (3.0) doesn't like my method and complains that "Incompatible pointer types returning 'Abstract *__strong' from a function with result 'Concrete1*'". This warning (fortunately not an error) seems mistaken to me. Here is the source code:
has a class method that allocates an instance of another class Variant. Both classes are subclasses of a common superclass Abstract. This method looks like this:
// override in class Concrete1:
+ (Abstract*) newAbstractWithParameters:(NSDictionary *)params inContext:(NSManagedObject *)moc
{
Abstract*result = nil;
<some block of code looking at params and allocating the correct concrete Abstract subclass corresponding to those parameters>
return result; // the warning occurs here
}
It seems to me that clang is mistaken in believing that this +[newAbstractWithParameters: inContext:] method has result Concrete1*. It hasn't.
A quick experiment also showed that the warning disappears if I rename the method with a name not starting in "new".
So I suspect something too aggressive in assumptions that clang makes in its static analyzer. Perhaps it assume that any class method starting with new returns an instance of the class it's defined in. Assumption which is incorrect here. Or something.
In any case, what would be the most sensible way to shut up that warning for that method only? I'd rather not disable the incompatible-pointer-types warning altogether for my target.
Note for the sake of completeness that the Class object to which the +[newAbstractWithParameters: inContext:] message is sent is computed at run time. The whole context is a DSL interpreter. All the classes are Core Data entities, but I can't see how this might change anything.
And for reference, the Abstract implementation of that methods is rather trivial:
+ (Abstract*) newAbstractWithParameters:(NSDictionary *)params inContext:(NSManagedObject *)moc
{
Abstract*result = [self insertInManagedObjectContext:moc]; // more or less the same as alloc
[result initFromDictionary:params];
return result; // the warning occurs here
}
So indeed I use class polymorphism here.
Thanks for any suggestion.
Jean-Denis
_______________________________________________
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