blob: e7ff5e1e22327f5121e8010d9cb8d9c0ea33b82b [file] [log] [blame]
George Karpenkove99ba6e2018-08-10 22:27:04 +00001// RUN: %clang_analyze_cc1 -fblocks -analyze -analyzer-checker=core,nullability,apiModeling,debug.ExprInspection -verify %s
George Karpenkov2301c5a2018-03-23 00:16:03 +00002
3#include "Inputs/system-header-simulator-for-nullability.h"
4
George Karpenkove99ba6e2018-08-10 22:27:04 +00005void clang_analyzer_warnIfReached();
6
George Karpenkov2301c5a2018-03-23 00:16:03 +00007NSString* _Nonnull trust_nonnull_framework_annotation() {
8 NSString* out = [NSString generateString];
9 if (out) {}
10 return out; // no-warning
11}
12
George Karpenkov7744c7f2018-05-31 00:28:13 +000013NSString* _Nonnull trust_instancemsg_annotation(NSString* _Nonnull param) {
14 NSString* out = [param stringByAppendingString:@"string"];
15 if (out) {}
16 return out; // no-warning
17}
18
19NSString* _Nonnull distrust_instancemsg_noannotation(NSString* param) {
20 if (param) {}
21 NSString* out = [param stringByAppendingString:@"string"];
22 if (out) {}
23 return out; // expected-warning{{}}
24}
25
26NSString* _Nonnull trust_analyzer_knowledge(NSString* param) {
27 if (!param)
28 return @"";
29 NSString* out = [param stringByAppendingString:@"string"];
30 if (out) {}
31 return out; // no-warning
32}
33
34NSString* _Nonnull trust_assume_nonnull_macro() {
35 NSString* out = [NSString generateImplicitlyNonnullString];
36 if (out) {}
37 return out; // no-warning
38}
39
George Karpenkov2301c5a2018-03-23 00:16:03 +000040NSString* _Nonnull distrust_without_annotation() {
41 NSString* out = [NSString generatePossiblyNullString];
42 if (out) {}
43 return out; // expected-warning{{}}
44}
45
46NSString* _Nonnull nonnull_please_trust_me();
47
48NSString* _Nonnull distrust_local_nonnull_annotation() {
49 NSString* out = nonnull_please_trust_me();
50 if (out) {}
51 return out; // expected-warning{{}}
52}
53
54NSString* _Nonnull trust_c_function() {
55 NSString* out = getString();
56 if (out) {};
57 return out; // no-warning
58}
59
60NSString* _Nonnull distrust_unannoted_function() {
61 NSString* out = getPossiblyNullString();
62 if (out) {};
63 return out; // expected-warning{{}}
64}
65
66NSString * _Nonnull distrustProtocol(id<MyProtocol> o) {
67 NSString* out = [o getString];
68 if (out) {};
69 return out; // expected-warning{{}}
70}
George Karpenkov7744c7f2018-05-31 00:28:13 +000071
George Karpenkove99ba6e2018-08-10 22:27:04 +000072// If the return value is non-nil, the index is non-nil.
73NSString *_Nonnull retImpliesIndex(NSString *s,
74 NSDictionary *dic) {
75 id obj = dic[s];
76 if (s) {}
77 if (obj)
78 return s; // no-warning
79 return @"foo";
80}
81
82NSString *_Nonnull retImpliesIndexOtherMethod(NSString *s,
83 NSDictionary *dic) {
84 id obj = [dic objectForKey:s];
85 if (s) {}
86 if (obj)
87 return s; // no-warning
88 return @"foo";
89}
90
91NSString *_Nonnull retImpliesIndexOnRHS(NSString *s,
92 NSDictionary *dic) {
93 id obj = dic[s];
94 if (s) {}
95 if (nil != obj)
96 return s; // no-warning
97 return @"foo";
98}
99
100NSString *_Nonnull retImpliesIndexReverseCheck(NSString *s,
101 NSDictionary *dic) {
102 id obj = dic[s];
103 if (s) {}
104 if (!obj)
105 return @"foo";
106 return s; // no-warning
107}
108
109NSString *_Nonnull retImpliesIndexReverseCheckOnRHS(NSString *s,
110 NSDictionary *dic) {
111 id obj = dic[s];
112 if (s) {}
113 if (nil == obj)
114 return @"foo";
115 return s; // no-warning
116}
117
118NSString *_Nonnull retImpliesIndexWrongBranch(NSString *s,
119 NSDictionary *dic) {
120 id obj = dic[s];
121 if (s) {}
122 if (!obj)
123 return s; // expected-warning{{}}
124 return @"foo";
125}
126
127NSString *_Nonnull retImpliesIndexWrongBranchOnRHS(NSString *s,
128 NSDictionary *dic) {
129 id obj = dic[s];
130 if (s) {}
131 if (nil == obj)
132 return s; // expected-warning{{}}
133 return @"foo";
134}
135
136// The return value could still be nil for a non-nil index.
137NSDictionary *_Nonnull indexDoesNotImplyRet(NSString *s,
138 NSDictionary *dic) {
139 id obj = dic[s];
140 if (obj) {}
141 if (s)
142 return obj; // expected-warning{{}}
143 return [[NSDictionary alloc] init];
144}
145
146// The return value could still be nil for a non-nil index.
147NSDictionary *_Nonnull notIndexImpliesNotRet(NSString *s,
148 NSDictionary *dic) {
149 id obj = dic[s];
150 if (!s) {
151 if (obj != nil) {
152 clang_analyzer_warnIfReached(); // no-warning
153 }
154 }
155 return [[NSDictionary alloc] init];
156}
157
158NSString *_Nonnull checkAssumeOnMutableDictionary(NSMutableDictionary *d,
159 NSString *k,
160 NSString *val) {
161 d[k] = val;
162 if (k) {}
163 return k; // no-warning
164}
165
166NSString *_Nonnull checkAssumeOnMutableDictionaryOtherMethod(NSMutableDictionary *d,
167 NSString *k,
168 NSString *val) {
169 [d setObject:val forKey:k];
170 if (k) {}
171 return k; // no-warning
172}