1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,nullability -verify %s
8 - (instancetype)autorelease;
12 __attribute__((objc_root_class))
17 @interface TestObject : NSObject
20 TestObject *_Nonnull returnsNilObjCInstanceIndirectly() {
21 TestObject *local = nil;
22 return local; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
25 TestObject * _Nonnull returnsNilObjCInstanceIndirectlyWithSupressingCast() {
26 TestObject *local = nil;
27 return (TestObject * _Nonnull)local; // no-warning
30 TestObject * _Nonnull returnsNilObjCInstanceDirectly() {
31 // The first warning is from Sema. The second is from the static analyzer.
32 return nil; // expected-warning {{null returned from function that requires a non-null return value}}
33 // expected-warning@-1 {{nil returned from a function that is expected to return a non-null value}}
36 TestObject * _Nonnull returnsNilObjCInstanceDirectlyWithSuppressingCast() {
37 return (TestObject * _Nonnull)nil; // no-warning
40 void testObjCNonARCNoInitialization(TestObject * _Nonnull p) {
41 TestObject * _Nonnull implicitlyZeroInitialized; // no-warning
42 implicitlyZeroInitialized = p;
45 void testObjCNonARCExplicitZeroInitialization() {
46 TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{nil assigned to a pointer which is expected to have non-null value}}
49 @interface ClassWithInitializers : NSObject
52 @implementation ClassWithInitializers
53 - (instancetype _Nonnull)initWithNonnullReturnAndSelfCheckingIdiom {
54 // This defensive check is a common-enough idiom that we don't want
55 // to issue a diagnostic for it.
56 if (self = [super init]) {
59 return self; // no-warning
62 - (instancetype _Nonnull)initWithNonnullReturnAndNilReturnViaLocal {
64 // This leaks, but we're not checking for that here.
66 ClassWithInitializers *other = nil;
67 // False negative. Once we have more subtle suppression of defensive checks in
68 // initializers we should warn here.
72 - (instancetype _Nonnull)initWithPreconditionViolation:(int)p {
76 return (ClassWithInitializers * _Nonnull)nil;
81 + (instancetype _Nonnull)factoryCallingInitWithNonnullReturnAndSelfCheckingIdiom {
82 return [[[self alloc] initWithNonnullReturnAndSelfCheckingIdiom] autorelease]; // no-warning
85 + (instancetype _Nonnull)factoryCallingInitWithNonnullReturnAndNilReturnViaLocal {
86 return [[[self alloc] initWithNonnullReturnAndNilReturnViaLocal] autorelease]; // no-warning
89 + (instancetype _Nonnull)initWithPreconditionViolation:(int) p {
90 return [[[self alloc] initWithPreconditionViolation:p] autorelease]; // no-warning
93 - (TestObject * _Nonnull) returnsNil {
94 return (TestObject * _Nonnull)nil;
96 - (TestObject * _Nonnull) inlineOfReturnsNilObjCInstanceDirectlyWithSuppressingCast {
97 TestObject *o = [self returnsNil];