[PATCH] D40528: add new check to find NSError init invocation

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 29 02:45:22 PST 2017


hokein added inline comments.


================
Comment at: clang-tidy/objc/AvoidNserrorInitCheck.cpp:31
+  diag(MatchedExpr->getLocStart(),
+       "use errorWithDomain:code:userInfo: to create a new NSError");
+}
----------------
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.



================
Comment at: clang-tidy/objc/AvoidNserrorInitCheck.h:24
+/// http://clang.llvm.org/extra/clang-tidy/checks/objc-avoid-nserror-init.html
+class AvoidNserrorInitCheck : public ClangTidyCheck {
+ public:
----------------
I'd name it `AvoidNSErrorInitCheck`.


================
Comment at: docs/clang-tidy/checks/objc-avoid-nserror-init.rst:10
+``errorWithDomain:code:userInfo:`` to create new NSError objects instead
+of ``[NSError alloc] init]``. Otherwise it will lead to a warning message
+during compilation in Xcode.
----------------
Wizard wrote:
> hokein wrote:
> > What's the warning message in Xcode? I suspect whether there is a diagnostic flag in clang already. 
> It was discussed originally here https://buganizer.corp.google.com/issues/62445078 I think
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.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D40528





More information about the cfe-commits mailing list