summaryrefslogtreecommitdiffstats
path: root/test/Analysis/nullability-no-arc.mm
blob: 0760186f42433dfd1c92718941bb5874729b6d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// RUN: %clang_analyze_cc1 -analyzer-checker=core,nullability -verify %s

#define nil 0

@protocol NSObject
+ (id)alloc;
- (id)init;
- (instancetype)autorelease;
- (void)release;
@end

__attribute__((objc_root_class))
@interface
NSObject<NSObject>
@end

@interface TestObject : NSObject
@end

TestObject *_Nonnull returnsNilObjCInstanceIndirectly() {
  TestObject *local = nil;
  return local; // expected-warning {{nil returned from a function that is expected to return a non-null value}}
}

TestObject * _Nonnull returnsNilObjCInstanceIndirectlyWithSupressingCast() {
  TestObject *local = nil;
  return (TestObject * _Nonnull)local; // no-warning
}

TestObject * _Nonnull returnsNilObjCInstanceDirectly() {
  // The first warning is from Sema. The second is from the static analyzer.
  return nil; // expected-warning {{null returned from function that requires a non-null return value}}
              // expected-warning@-1 {{nil returned from a function that is expected to return a non-null value}}
}

TestObject * _Nonnull returnsNilObjCInstanceDirectlyWithSuppressingCast() {
  return (TestObject * _Nonnull)nil; // no-warning
}

void testObjCNonARCNoInitialization(TestObject * _Nonnull p) {
  TestObject * _Nonnull implicitlyZeroInitialized; // no-warning
  implicitlyZeroInitialized = p;
}

void testObjCNonARCExplicitZeroInitialization() {
  TestObject * _Nonnull explicitlyZeroInitialized = nil; // expected-warning {{nil assigned to a pointer which is expected to have non-null value}}
}

@interface ClassWithInitializers : NSObject
@end

@implementation ClassWithInitializers
- (instancetype _Nonnull)initWithNonnullReturnAndSelfCheckingIdiom {
  // This defensive check is a common-enough idiom that we don't want
  // to issue a diagnostic for it.
  if (self = [super init]) {
  }

  return self; // no-warning
}

- (instancetype _Nonnull)initWithNonnullReturnAndNilReturnViaLocal {
  self = [super init];
  // This leaks, but we're not checking for that here.

  ClassWithInitializers *other = nil;
  // False negative. Once we have more subtle suppression of defensive checks in
  // initializers we should warn here.
  return other;
}

- (instancetype _Nonnull)initWithPreconditionViolation:(int)p {
  self = [super init];
  if (p < 0) {
    [self release];
    return (ClassWithInitializers * _Nonnull)nil;
  }
  return self;
}

+ (instancetype _Nonnull)factoryCallingInitWithNonnullReturnAndSelfCheckingIdiom {
  return [[[self alloc] initWithNonnullReturnAndSelfCheckingIdiom] autorelease]; // no-warning
}

+ (instancetype _Nonnull)factoryCallingInitWithNonnullReturnAndNilReturnViaLocal {
  return [[[self alloc] initWithNonnullReturnAndNilReturnViaLocal] autorelease]; // no-warning
}

+ (instancetype _Nonnull)initWithPreconditionViolation:(int) p {
  return [[[self alloc] initWithPreconditionViolation:p] autorelease]; // no-warning
}

- (TestObject * _Nonnull) returnsNil {
  return (TestObject * _Nonnull)nil;
}
- (TestObject * _Nonnull) inlineOfReturnsNilObjCInstanceDirectlyWithSuppressingCast {
  TestObject *o = [self returnsNil];
  return o;
}
@end