[cfe-dev] Obj-C false positive warning with deprecation and instancetype?

jahanian fjahanian at apple.com
Wed Jan 11 10:49:32 PST 2012


On Jan 11, 2012, at 8:10 AM, Sean McBride wrote:

> Hi all,
> 
> Using clang r147866 on Mac OS X 10.7, the code below causes this warning:
> 
> $ clang -fsyntax-only test.m 
> test.m:18:11: warning: 'initWithContentsOfURL:' is deprecated [-Wdeprecated-declarations]
>        return [[[self alloc] initWithContentsOfURL:inURL] autorelease];
>                 ^
> 
> NSString has a deprecated method initWithContentsOfURL:, but in my own class it's not deprecated.  I would have thought that between my own use of instancetype and clang's special casing of alloc that this should not warn.  Is it a false positive bug?

It is not false positive. [self alloc] is of type 'id'. Not knowing which initWithContentsOfURL gets called, clang goes out of its way to issue the deprecated warning so user can take action.
You can make your intention known to clang by type-casting [self alloc].

- Fariborz

> 
> -----------
> #import <Foundation/Foundation.h>
> 
> @interface Awesome : NSObject { }
> - (instancetype)initWithContentsOfURL:(NSURL*)inURL; // designated initializer
> + (instancetype)awsomeWithContentsOfURL:(NSURL*)inURL;
> @end
> 
> @implementation Awesome
> 
> - (instancetype)initWithContentsOfURL:(NSURL*)inURL
> {
> 	self = [super init];
> 	return self;
> }
> 
> + (instancetype)awsomeWithContentsOfURL:(NSURL*)inURL
> {
> 	return [[[self alloc] initWithContentsOfURL:inURL] autorelease];
> }
> 
> @end
> 
> int main (int argc, char *argv[])
> {
>    (void)argc; (void)argv;
>    return 0;
> }
> -----------
> 
> Thanks,
> 
> Sean
> 
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list