Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fblocks -analyze -analyzer-checker=core,nullability -verify %s |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 2 | |
| 3 | #define nil 0 |
| 4 | #define BOOL int |
| 5 | |
Devin Coughlin | de21767 | 2016-01-28 22:23:34 +0000 | [diff] [blame] | 6 | #define NS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin") |
| 7 | #define NS_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end") |
| 8 | |
| 9 | typedef struct _NSZone NSZone; |
| 10 | |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 11 | @protocol NSObject |
| 12 | + (id)alloc; |
| 13 | - (id)init; |
| 14 | @end |
| 15 | |
Devin Coughlin | de21767 | 2016-01-28 22:23:34 +0000 | [diff] [blame] | 16 | NS_ASSUME_NONNULL_BEGIN |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 17 | @protocol NSCopying |
Devin Coughlin | de21767 | 2016-01-28 22:23:34 +0000 | [diff] [blame] | 18 | - (id)copyWithZone:(nullable NSZone *)zone; |
| 19 | |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 20 | @end |
| 21 | |
Devin Coughlin | de21767 | 2016-01-28 22:23:34 +0000 | [diff] [blame] | 22 | @protocol NSMutableCopying |
| 23 | - (id)mutableCopyWithZone:(nullable NSZone *)zone; |
| 24 | @end |
| 25 | NS_ASSUME_NONNULL_END |
| 26 | |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 27 | __attribute__((objc_root_class)) |
| 28 | @interface |
| 29 | NSObject<NSObject> |
| 30 | @end |
| 31 | |
| 32 | @interface NSString : NSObject<NSCopying> |
| 33 | - (BOOL)isEqualToString : (NSString *_Nonnull)aString; |
| 34 | - (NSString *)stringByAppendingString:(NSString *_Nonnull)aString; |
| 35 | @end |
| 36 | |
| 37 | @interface TestObject : NSObject |
| 38 | - (int *_Nonnull)returnsNonnull; |
| 39 | - (int *_Nullable)returnsNullable; |
| 40 | - (int *)returnsUnspecified; |
| 41 | - (void)takesNonnull:(int *_Nonnull)p; |
| 42 | - (void)takesNullable:(int *_Nullable)p; |
| 43 | - (void)takesUnspecified:(int *)p; |
| 44 | @property(readonly, strong) NSString *stuff; |
| 45 | @end |
| 46 | |
| 47 | TestObject * getUnspecifiedTestObject(); |
| 48 | TestObject *_Nonnull getNonnullTestObject(); |
| 49 | TestObject *_Nullable getNullableTestObject(); |
| 50 | |
| 51 | int getRandom(); |
| 52 | |
| 53 | typedef struct Dummy { int val; } Dummy; |
| 54 | |
| 55 | void takesNullable(Dummy *_Nullable); |
| 56 | void takesNonnull(Dummy *_Nonnull); |
| 57 | void takesUnspecified(Dummy *); |
| 58 | |
| 59 | Dummy *_Nullable returnsNullable(); |
| 60 | Dummy *_Nonnull returnsNonnull(); |
| 61 | Dummy *returnsUnspecified(); |
| 62 | int *_Nullable returnsNullableInt(); |
| 63 | |
| 64 | template <typename T> T *eraseNullab(T *p) { return p; } |
| 65 | |
Gabor Horvath | 8d3ad6b | 2015-08-27 18:49:07 +0000 | [diff] [blame] | 66 | void takesAttrNonnull(Dummy *p) __attribute((nonnull(1))); |
| 67 | |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 68 | void testBasicRules() { |
| 69 | Dummy *p = returnsNullable(); |
| 70 | int *ptr = returnsNullableInt(); |
| 71 | // Make every dereference a different path to avoid sinks after errors. |
| 72 | switch (getRandom()) { |
| 73 | case 0: { |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 74 | Dummy &r = *p; // expected-warning {{Nullable pointer is dereferenced}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 75 | } break; |
| 76 | case 1: { |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 77 | int b = p->val; // expected-warning {{Nullable pointer is dereferenced}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 78 | } break; |
| 79 | case 2: { |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 80 | int stuff = *ptr; // expected-warning {{Nullable pointer is dereferenced}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 81 | } break; |
| 82 | case 3: |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 83 | takesNonnull(p); // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 84 | break; |
| 85 | case 4: { |
| 86 | Dummy d; |
| 87 | takesNullable(&d); |
| 88 | Dummy dd(d); |
| 89 | break; |
| 90 | } |
Gabor Horvath | 8d3ad6b | 2015-08-27 18:49:07 +0000 | [diff] [blame] | 91 | case 5: takesAttrNonnull(p); break; // expected-warning {{Nullable pointer is passed to}} |
| 92 | default: { Dummy d = *p; } break; // expected-warning {{Nullable pointer is dereferenced}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 93 | } |
| 94 | if (p) { |
| 95 | takesNonnull(p); |
| 96 | if (getRandom()) { |
| 97 | Dummy &r = *p; |
| 98 | } else { |
| 99 | int b = p->val; |
| 100 | } |
| 101 | } |
| 102 | Dummy *q = 0; |
| 103 | if (getRandom()) { |
| 104 | takesNullable(q); |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 105 | takesNonnull(q); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 106 | } |
| 107 | Dummy a; |
| 108 | Dummy *_Nonnull nonnull = &a; |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 109 | nonnull = q; // expected-warning {{Null is assigned to a pointer which is expected to have non-null value}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 110 | q = &a; |
| 111 | takesNullable(q); |
| 112 | takesNonnull(q); |
| 113 | } |
| 114 | |
| 115 | void testMultiParamChecking(Dummy *_Nonnull a, Dummy *_Nullable b, |
| 116 | Dummy *_Nonnull c); |
| 117 | |
| 118 | void testArgumentTracking(Dummy *_Nonnull nonnull, Dummy *_Nullable nullable) { |
| 119 | Dummy *p = nullable; |
| 120 | Dummy *q = nonnull; |
| 121 | switch(getRandom()) { |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 122 | case 1: nonnull = p; break; // expected-warning {{Nullable pointer is assigned to a pointer which is expected to have non-null value}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 123 | case 2: p = 0; break; |
| 124 | case 3: q = p; break; |
| 125 | case 4: testMultiParamChecking(nonnull, nullable, nonnull); break; |
| 126 | case 5: testMultiParamChecking(nonnull, nonnull, nonnull); break; |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 127 | case 6: testMultiParamChecking(nonnull, nullable, nullable); break; // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 3rd parameter}} |
| 128 | case 7: testMultiParamChecking(nullable, nullable, nonnull); // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
| 129 | case 8: testMultiParamChecking(nullable, nullable, nullable); // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 130 | case 9: testMultiParamChecking((Dummy *_Nonnull)0, nullable, nonnull); break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | Dummy *_Nonnull testNullableReturn(Dummy *_Nullable a) { |
| 135 | Dummy *p = a; |
Devin Coughlin | 5a3843e | 2016-01-18 18:53:33 +0000 | [diff] [blame] | 136 | return p; // expected-warning {{Nullable pointer is returned from a function that is expected to return a non-null value}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | Dummy *_Nonnull testNullReturn() { |
| 140 | Dummy *p = 0; |
Devin Coughlin | 5a3843e | 2016-01-18 18:53:33 +0000 | [diff] [blame] | 141 | return p; // expected-warning {{Null is returned from a function that is expected to return a non-null value}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void testObjCMessageResultNullability() { |
| 145 | // The expected result: the most nullable of self and method return type. |
| 146 | TestObject *o = getUnspecifiedTestObject(); |
| 147 | int *shouldBeNullable = [eraseNullab(getNullableTestObject()) returnsNonnull]; |
| 148 | switch (getRandom()) { |
| 149 | case 0: |
| 150 | // The core analyzer assumes that the receiver is non-null after a message |
| 151 | // send. This is to avoid some false positives, and increase performance |
| 152 | // but it also reduces the coverage and makes this checker unable to reason |
| 153 | // about the nullness of the receiver. |
| 154 | [o takesNonnull:shouldBeNullable]; // No warning expected. |
| 155 | break; |
| 156 | case 1: |
| 157 | shouldBeNullable = |
| 158 | [eraseNullab(getNullableTestObject()) returnsUnspecified]; |
| 159 | [o takesNonnull:shouldBeNullable]; // No warning expected. |
| 160 | break; |
| 161 | case 3: |
| 162 | shouldBeNullable = [eraseNullab(getNullableTestObject()) returnsNullable]; |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 163 | [o takesNonnull:shouldBeNullable]; // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 164 | break; |
| 165 | case 4: |
| 166 | shouldBeNullable = [eraseNullab(getNonnullTestObject()) returnsNullable]; |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 167 | [o takesNonnull:shouldBeNullable]; // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 168 | break; |
| 169 | case 5: |
| 170 | shouldBeNullable = |
| 171 | [eraseNullab(getUnspecifiedTestObject()) returnsNullable]; |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 172 | [o takesNonnull:shouldBeNullable]; // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 173 | break; |
| 174 | case 6: |
| 175 | shouldBeNullable = [eraseNullab(getNullableTestObject()) returnsNullable]; |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 176 | [o takesNonnull:shouldBeNullable]; // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 177 | break; |
| 178 | case 7: { |
| 179 | int *shouldBeNonnull = [eraseNullab(getNonnullTestObject()) returnsNonnull]; |
| 180 | [o takesNonnull:shouldBeNonnull]; |
| 181 | } break; |
| 182 | } |
| 183 | } |
| 184 | |
Devin Coughlin | 755baa4 | 2015-12-29 17:40:49 +0000 | [diff] [blame] | 185 | Dummy * _Nonnull testDirectCastNullableToNonnull() { |
| 186 | Dummy *p = returnsNullable(); |
| 187 | takesNonnull((Dummy * _Nonnull)p); // no-warning |
| 188 | return (Dummy * _Nonnull)p; // no-warning |
| 189 | } |
| 190 | |
| 191 | Dummy * _Nonnull testIndirectCastNullableToNonnull() { |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 192 | Dummy *p = (Dummy * _Nonnull)returnsNullable(); |
Devin Coughlin | 755baa4 | 2015-12-29 17:40:49 +0000 | [diff] [blame] | 193 | takesNonnull(p); // no-warning |
| 194 | return p; // no-warning |
| 195 | } |
| 196 | |
| 197 | Dummy * _Nonnull testDirectCastNilToNonnull() { |
| 198 | takesNonnull((Dummy * _Nonnull)0); // no-warning |
| 199 | return (Dummy * _Nonnull)0; // no-warning |
| 200 | } |
| 201 | |
| 202 | void testIndirectCastNilToNonnullAndPass() { |
| 203 | Dummy *p = (Dummy * _Nonnull)0; |
| 204 | // FIXME: Ideally the cast above would suppress this warning. |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 205 | takesNonnull(p); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}} |
| 206 | } |
| 207 | |
| 208 | void testIndirectNilPassToNonnull() { |
| 209 | Dummy *p = 0; |
| 210 | takesNonnull(p); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}} |
| 211 | } |
| 212 | |
| 213 | void testConditionalNilPassToNonnull(Dummy *p) { |
| 214 | if (!p) { |
| 215 | takesNonnull(p); // expected-warning {{Null passed to a callee that requires a non-null 1st parameter}} |
| 216 | } |
Devin Coughlin | 755baa4 | 2015-12-29 17:40:49 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | Dummy * _Nonnull testIndirectCastNilToNonnullAndReturn() { |
| 220 | Dummy *p = (Dummy * _Nonnull)0; |
| 221 | // FIXME: Ideally the cast above would suppress this warning. |
| 222 | return p; // expected-warning {{Null is returned from a function that is expected to return a non-null value}} |
Gabor Horvath | 2869092 | 2015-08-26 23:17:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void testInvalidPropagation() { |
| 226 | Dummy *p = returnsUnspecified(); |
| 227 | takesNullable(p); |
| 228 | takesNonnull(p); |
| 229 | } |
Gabor Horvath | b47128a | 2015-09-03 23:16:21 +0000 | [diff] [blame] | 230 | |
| 231 | void onlyReportFirstPreconditionViolationOnPath() { |
| 232 | Dummy *p = returnsNullable(); |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 233 | takesNonnull(p); // expected-warning {{Nullable pointer is passed to a callee that requires a non-null 1st parameter}} |
Gabor Horvath | b47128a | 2015-09-03 23:16:21 +0000 | [diff] [blame] | 234 | takesNonnull(p); // No warning. |
| 235 | // The first warning was not a sink. The analysis expected to continue. |
| 236 | int i = 0; |
| 237 | i = 5 / i; // expected-warning {{Division by zero}} |
| 238 | (void)i; |
| 239 | } |
| 240 | |
| 241 | Dummy *_Nonnull doNotWarnWhenPreconditionIsViolatedInTopFunc( |
| 242 | Dummy *_Nonnull p) { |
| 243 | if (!p) { |
| 244 | Dummy *ret = |
| 245 | 0; // avoid compiler warning (which is not generated by the analyzer) |
| 246 | if (getRandom()) |
| 247 | return ret; // no warning |
| 248 | else |
| 249 | return p; // no warning |
| 250 | } else { |
| 251 | return p; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | Dummy *_Nonnull doNotWarnWhenPreconditionIsViolated(Dummy *_Nonnull p) { |
| 256 | if (!p) { |
| 257 | Dummy *ret = |
| 258 | 0; // avoid compiler warning (which is not generated by the analyzer) |
| 259 | if (getRandom()) |
| 260 | return ret; // no warning |
| 261 | else |
| 262 | return p; // no warning |
| 263 | } else { |
| 264 | return p; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void testPreconditionViolationInInlinedFunction(Dummy *p) { |
| 269 | doNotWarnWhenPreconditionIsViolated(p); |
| 270 | } |
| 271 | |
| 272 | void inlinedNullable(Dummy *_Nullable p) { |
| 273 | if (p) return; |
| 274 | } |
| 275 | void inlinedNonnull(Dummy *_Nonnull p) { |
| 276 | if (p) return; |
| 277 | } |
| 278 | void inlinedUnspecified(Dummy *p) { |
| 279 | if (p) return; |
| 280 | } |
| 281 | |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 282 | void testNilReturnWithBlock(Dummy *p) { |
| 283 | p = 0; |
| 284 | Dummy *_Nonnull (^myblock)(void) = ^Dummy *_Nonnull(void) { |
| 285 | return p; // TODO: We should warn in blocks. |
| 286 | }; |
| 287 | myblock(); |
| 288 | } |
| 289 | |
Gabor Horvath | b47128a | 2015-09-03 23:16:21 +0000 | [diff] [blame] | 290 | Dummy *_Nonnull testDefensiveInlineChecks(Dummy * p) { |
| 291 | switch (getRandom()) { |
| 292 | case 1: inlinedNullable(p); break; |
| 293 | case 2: inlinedNonnull(p); break; |
| 294 | case 3: inlinedUnspecified(p); break; |
| 295 | } |
| 296 | if (getRandom()) |
Devin Coughlin | c198663 | 2015-11-24 19:15:11 +0000 | [diff] [blame] | 297 | takesNonnull(p); // no-warning |
| 298 | |
| 299 | if (getRandom()) { |
| 300 | Dummy *_Nonnull varWithInitializer = p; // no-warning |
| 301 | |
| 302 | Dummy *_Nonnull var1WithInitializer = p, // no-warning |
| 303 | *_Nonnull var2WithInitializer = p; // no-warning |
| 304 | } |
| 305 | |
| 306 | if (getRandom()) { |
| 307 | Dummy *_Nonnull varWithoutInitializer; |
| 308 | varWithoutInitializer = p; // no-warning |
| 309 | } |
| 310 | |
Gabor Horvath | b47128a | 2015-09-03 23:16:21 +0000 | [diff] [blame] | 311 | return p; |
| 312 | } |
Devin Coughlin | 3ab8b2e7 | 2015-12-29 23:44:19 +0000 | [diff] [blame] | 313 | |
Devin Coughlin | 4a33020 | 2016-01-22 01:01:11 +0000 | [diff] [blame] | 314 | |
| 315 | @interface SomeClass : NSObject { |
| 316 | int instanceVar; |
| 317 | } |
Devin Coughlin | 5a3843e | 2016-01-18 18:53:33 +0000 | [diff] [blame] | 318 | @end |
| 319 | |
| 320 | @implementation SomeClass (MethodReturn) |
Devin Coughlin | 4a33020 | 2016-01-22 01:01:11 +0000 | [diff] [blame] | 321 | - (id)initWithSomething:(int)i { |
| 322 | if (self = [super init]) { |
| 323 | instanceVar = i; |
| 324 | } |
| 325 | |
| 326 | return self; |
| 327 | } |
| 328 | |
Devin Coughlin | 5a3843e | 2016-01-18 18:53:33 +0000 | [diff] [blame] | 329 | - (TestObject * _Nonnull)testReturnsNullableInNonnullIndirectly { |
| 330 | TestObject *local = getNullableTestObject(); |
Anna Zaks | ad9e7ea | 2016-01-29 18:43:15 +0000 | [diff] [blame^] | 331 | return local; // expected-warning {{Nullable pointer is returned from a method that is expected to return a non-null value}} |
Devin Coughlin | 3ab8b2e7 | 2015-12-29 23:44:19 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Devin Coughlin | 5a3843e | 2016-01-18 18:53:33 +0000 | [diff] [blame] | 334 | - (TestObject * _Nonnull)testReturnsCastSuppressedNullableInNonnullIndirectly { |
| 335 | TestObject *local = getNullableTestObject(); |
| 336 | return (TestObject * _Nonnull)local; // no-warning |
Devin Coughlin | 3ab8b2e7 | 2015-12-29 23:44:19 +0000 | [diff] [blame] | 337 | } |
Devin Coughlin | 5a3843e | 2016-01-18 18:53:33 +0000 | [diff] [blame] | 338 | |
| 339 | - (TestObject * _Nonnull)testReturnsNullableInNonnullWhenPreconditionViolated:(TestObject * _Nonnull) p { |
| 340 | TestObject *local = getNullableTestObject(); |
| 341 | if (!p) // Pre-condition violated here. |
| 342 | return local; // no-warning |
| 343 | else |
| 344 | return p; // no-warning |
| 345 | } |
| 346 | @end |
Devin Coughlin | 4a33020 | 2016-01-22 01:01:11 +0000 | [diff] [blame] | 347 | |
| 348 | @interface ClassWithInitializers : NSObject |
| 349 | @end |
| 350 | |
| 351 | @implementation ClassWithInitializers |
| 352 | - (instancetype _Nonnull)initWithNonnullReturnAndSelfCheckingIdiom { |
| 353 | // This defensive check is a common-enough idiom that we filter don't want |
| 354 | // to issue a diagnostic for it, |
| 355 | if (self = [super init]) { |
| 356 | } |
| 357 | |
| 358 | return self; // no-warning |
| 359 | } |
| 360 | |
| 361 | - (instancetype _Nonnull)initWithNonnullReturnAndNilReturnViaLocal { |
| 362 | self = [super init]; |
| 363 | // This leaks, but we're not checking for that here. |
| 364 | |
| 365 | ClassWithInitializers *other = nil; |
Devin Coughlin | de21767 | 2016-01-28 22:23:34 +0000 | [diff] [blame] | 366 | // False negative. Once we have more subtle suppression of defensive checks in |
| 367 | // initializers we should warn here. |
| 368 | return other; |
Devin Coughlin | 4a33020 | 2016-01-22 01:01:11 +0000 | [diff] [blame] | 369 | } |
| 370 | @end |
| 371 | |
| 372 | @interface SubClassWithInitializers : ClassWithInitializers |
| 373 | @end |
| 374 | |
| 375 | @implementation SubClassWithInitializers |
| 376 | // Note: Because this is overridding |
| 377 | // -[ClassWithInitializers initWithNonnullReturnAndSelfCheckingIdiom], |
| 378 | // the return type of this method becomes implicitly id _Nonnull. |
| 379 | - (id)initWithNonnullReturnAndSelfCheckingIdiom { |
| 380 | if (self = [super initWithNonnullReturnAndSelfCheckingIdiom]) { |
| 381 | } |
| 382 | |
| 383 | return self; // no-warning |
| 384 | } |
Devin Coughlin | de21767 | 2016-01-28 22:23:34 +0000 | [diff] [blame] | 385 | |
| 386 | - (id _Nonnull)initWithNonnullReturnAndSelfCheckingIdiomV2; { |
| 387 | // Another common return-checking idiom |
| 388 | self = [super initWithNonnullReturnAndSelfCheckingIdiom]; |
| 389 | if (!self) { |
| 390 | return nil; // no-warning |
| 391 | } |
| 392 | |
| 393 | return self; |
| 394 | } |
| 395 | @end |
| 396 | |
| 397 | @interface ClassWithCopyWithZone : NSObject<NSCopying,NSMutableCopying> { |
| 398 | id i; |
| 399 | } |
| 400 | |
| 401 | @end |
| 402 | |
| 403 | @implementation ClassWithCopyWithZone |
| 404 | -(id)copyWithZone:(NSZone *)zone { |
| 405 | ClassWithCopyWithZone *newInstance = [[ClassWithCopyWithZone alloc] init]; |
| 406 | if (!newInstance) |
| 407 | return nil; |
| 408 | |
| 409 | newInstance->i = i; |
| 410 | return newInstance; |
| 411 | } |
| 412 | |
| 413 | -(id)mutableCopyWithZone:(NSZone *)zone { |
| 414 | ClassWithCopyWithZone *newInstance = [[ClassWithCopyWithZone alloc] init]; |
| 415 | if (newInstance) { |
| 416 | newInstance->i = i; |
| 417 | } |
| 418 | |
| 419 | return newInstance; |
| 420 | } |
Devin Coughlin | 4a33020 | 2016-01-22 01:01:11 +0000 | [diff] [blame] | 421 | @end |