-
Notifications
You must be signed in to change notification settings - Fork 281
Add some nullables to methods & properties that can return nil #589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -46,7 +46,7 @@ NS_ASSUME_NONNULL_BEGIN | |||
/// type - the credential types allowed by the operation. | |||
/// URL - the URL the operation is authenticating against. | |||
/// userName - the user name provided by the operation. Can be nil, and might be ignored. | |||
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(NSString *)URL userName:(nullable NSString *)userName; | |||
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(nullable NSString *)URL userName:(nullable NSString *)userName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GTCredentialAcquireCallback
potentially calls this with a nil url.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The libgit2 documentation implies that it's never NULL. At least git_remote_connect
will return an error on a NULL
URL. It seems the only other user is proxy support, and in that case it's not clear (there's a check skipping NULLs in git_proxy_options_dup
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't even find where it's used, searching for git_cred_acquire_cb
etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the regex /->cred(entials|_acquire_cb)/
against libgit2 and got most of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it's probably supposed to be non-null, but to guarantee it I'd rather depend on someone who knows the code better.
Question : does NSAssert-ing that the return value is not nil makes the warning go away ? Some of those changes look strange, eg. GTOID having a nullable |
Yes it does. Would you like me to replace the null checks with |
Good question. From an API standpoint, I think asserting makes it cleaner. On the other hand, if it ever happens, we'll throw an exception at runtime, which is Not Cool. |
Let's think about the actual failure cases, then. For GTOID.SHA:
I think it's okay to assert in those cases. I'll proceed similarly with the others and see what you think. |
@@ -52,7 +52,7 @@ NS_ASSUME_NONNULL_BEGIN | |||
@property (nonatomic, readonly, strong) GTRepository *repository; | |||
@property (nonatomic, readonly) GTReferenceType referenceType; | |||
@property (nonatomic, readonly) const git_oid *git_oid; | |||
@property (nonatomic, strong, readonly) GTOID *OID; | |||
@property (nonatomic, strong, readonly, nullable) GTOID *OID; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git_reference_target
returns null if it's not a direct reference, so this should be nullable.
@@ -42,10 +42,10 @@ NS_ASSUME_NONNULL_BEGIN | |||
@property (nonatomic, readonly, copy, nullable) NSString *email; | |||
|
|||
/// The time when the action happened. | |||
@property (nonatomic, readonly, strong) NSDate *time; | |||
@property (nonatomic, readonly, strong, nullable) NSDate *time; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other properties are nullable, so this is consistent.
On the other hand, they all return nil if git_signature
is nil, and it's not nullable, and it doesn't look like it should ever be null.
But since I'm making this consistent with the other properties, I'll leave that as a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC it's done that way because libgit2 might fail to parse an object's headers (in some degenerate-odb cases), hence allowing some of them to be null.
@@ -42,10 +42,10 @@ NS_ASSUME_NONNULL_BEGIN | |||
@property (nonatomic, readonly, copy, nullable) NSString *email; | |||
|
|||
/// The time when the action happened. | |||
@property (nonatomic, readonly, strong) NSDate *time; | |||
@property (nonatomic, readonly, strong, nullable) NSDate *time; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC it's done that way because libgit2 might fail to parse an object's headers (in some degenerate-odb cases), hence allowing some of them to be null.
|
||
git_oid_fmt(SHA, self.git_oid); | ||
|
||
NSString *str = [[NSString alloc] initWithBytesNoCopy:SHA length:GIT_OID_HEXSZ encoding:NSUTF8StringEncoding freeWhenDone:YES]; | ||
if (str == nil) free(SHA); | ||
NSAssert(str != nil, @"Failed to allocate SHA string"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
char *SHA = git_oid_tostr_s(self.git_oid); // Stored in TLS
NSString *str = [[NSString alloc] initWithBytes:SHA length:GIT_OID_HEXSZ encoding:NSUTF8StringEncoding]; // So we copy the TLS data
NSAssert(str != nil, @"Failed to allocate SHA string");
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better!
@@ -46,7 +46,7 @@ NS_ASSUME_NONNULL_BEGIN | |||
/// type - the credential types allowed by the operation. | |||
/// URL - the URL the operation is authenticating against. | |||
/// userName - the user name provided by the operation. Can be nil, and might be ignored. | |||
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(NSString *)URL userName:(nullable NSString *)userName; | |||
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(nullable NSString *)URL userName:(nullable NSString *)userName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the regex /->cred(entials|_acquire_cb)/
against libgit2 and got most of them.
I see the Travis failure, but it doesn't seem to be related to my changes. |
(bump) Any further changes needed on this? |
char *SHA = git_oid_tostr_s(self.git_oid); | ||
NSString *str = [[NSString alloc] initWithBytes:SHA | ||
length:GIT_OID_HEXSZ | ||
encoding:NSUTF8StringEncoding]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crazy indentation here 😉 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like Xcode was doing tabs instead of spaces, at 2 spaces per tab. Fixed.
Another bump. Any more issues? |
Not that I can see. A local run of |
Xcode 8 now complains about methods and properties that return nil but are not declared
nullable
. This fixes all of those analyzer warnings.