<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/83606>83606</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
-Wnonnull doesn't prevent returning nil in the basic scenarios for objc code
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dryganets
</td>
</tr>
</table>
<pre>
Seems like there is no way to enforce nullability checks in objective c correctly.
I was using
`clang main.m -o main -Weverything -framework Foundation`
to compile the snippet below.
```
#import <Foundation/Foundation.h>
const static char* test = "test string";
NS_ASSUME_NONNULL_BEGIN
@interface A : NSObject
- (NSString *)getString;
- (NSString *) getConstString;
@property (nonatomic, strong) NSString *nilString;
@end
@implementation A
@dynamic nilString;
- (nonnull NSString *)getString {
return nil;
}
- (NSString *)getConstString {
return @(test);
}
- (NSString *)nilString {
return nil;
}
@end
static NSString* getStringCStyle(void) {
return nil;
}
NS_ASSUME_NONNULL_END
int main(void) {
@autoreleasepool {
A* a = [A new];
NSLog(@"nil string: %@, static string: %@, c-style: %@", [a getString], [a getConstString], getStringCStyle());
}
return 0;
}
```
The compilation output is:
```
main.m:21:12: warning: implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull' [-Wnullable-to-nonnull-conversion]
return @(test);
^
main.m:31:5: warning: null returned from function that requires a non-null return value [-Wnonnull]
return nil;
```
So apparently I can't return `nil` from the c-style function but I'm free to do so in the ObjectiveC properties and methods.
There is a check that prevents me to return nil indirectly but at the same time I could return nil directly.
```
clang --version
Apple clang version 15.0.0 (clang-1500.0.40.1)
Target: arm64-apple-darwin23.3.0
Thread model: posix
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVkuP4jgQ_jXmUkrkOIQOBw7pB6uWZpkDO5pjy3EK8I5jZ20Hln-_chweDcysBrVoYrs-11dfPcKdk1uNuCDFMyle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