This is an archive of the discontinued LLVM Phabricator instance.

add new check to find NSError init invocation
ClosedPublic

Authored by Wizard on Nov 27 2017, 3:36 PM.

Details

Summary

This check will find out improper initialization of NSError objects.

According to Apple developer document, we should always use factory method errorWithDomain:code:userInfo: to create new NSError objects instead of [NSError alloc] init]. Otherwise it will lead to a warning message during runtime in Xcode.

The corresponding information about NSError creation: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html

Event Timeline

Wizard created this revision.Nov 27 2017, 3:36 PM
Wizard edited the summary of this revision. (Show Details)Nov 27 2017, 3:38 PM
Wizard added reviewers: hokein, benhamilton.
Wizard updated this revision to Diff 124486.Nov 27 2017, 3:39 PM

new line for doc

hokein added inline comments.Nov 28 2017, 1:28 AM
docs/clang-tidy/checks/objc-avoid-nserror-init.rst
10

What's the warning message in Xcode? I suspect whether there is a diagnostic flag in clang already.

Wizard added inline comments.Nov 28 2017, 2:25 AM
docs/clang-tidy/checks/objc-avoid-nserror-init.rst
10

It was discussed originally here https://buganizer.corp.google.com/issues/62445078 I think

hokein added inline comments.Nov 29 2017, 2:45 AM
clang-tidy/objc/AvoidNserrorInitCheck.cpp
31

not sure what's the best message here.

From apple's document:

You create an error object either by allocating it and then initializing it with the initWithDomain:code:userInfo: method of NSError or by using the class factory method errorWithDomain:code:userInfo:.

errorWithDomain:code:userInfo: is not the only way to create an NSError.

clang-tidy/objc/AvoidNserrorInitCheck.h
24

I'd name it AvoidNSErrorInitCheck.

docs/clang-tidy/checks/objc-avoid-nserror-init.rst
10

Thanks. Please don't include any internal links next time.

Looks like the error message (https://stackoverflow.com/questions/33720042/why-does-nserror-alloc-init-in-xcode-throw-an-error) is shown during runtime, instead of compilation. Could you please confirm it, and update the document here?

[NSError init] called; this results in an invalid NSError instance. It will raise an exception in a future release. Please call errorWithDomain:code:userInfo: or initWithDomain:code:userInfo:. This message shown only once.

Wizard updated this revision to Diff 124818.Nov 29 2017, 2:23 PM
Wizard marked 3 inline comments as done.

address comments

docs/clang-tidy/checks/objc-avoid-nserror-init.rst
10

Oh yes it is shown during runtime. Will update the doc

Wizard edited the summary of this revision. (Show Details)Nov 29 2017, 2:23 PM
benhamilton accepted this revision.Nov 30 2017, 7:59 AM
This revision is now accepted and ready to land.Nov 30 2017, 7:59 AM
Wizard updated this revision to Diff 124971.Nov 30 2017, 11:03 AM

change file name cases

Wizard updated this revision to Diff 124972.Nov 30 2017, 11:05 AM

restore file names

This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.