This is a small check to avoid throwing objc exceptions.
In specific it will detect the usage of @throw statement and throw warning.
Details
- Reviewers
hokein benhamilton - Commits
- rG999458139555: add check to avoid throwing objc exception according to Google Objective-C guide
rCTE318366: add check to avoid throwing objc exception according to Google Objective-C guide
rL318366: add check to avoid throwing objc exception according to Google Objective-C guide
Diff Detail
- Repository
- rL LLVM
Event Timeline
LGTM with a few nits, I'd like to see whether @benhamilton has comments before committing it.
docs/clang-tidy/checks/google-objc-avoid-throwing-exception.rst | ||
---|---|---|
4 ↗ | (On Diff #122953) | nit: remove the extra === |
10 ↗ | (On Diff #122953) | Use the public link: http://google.github.io/styleguide/objcguide.html#avoid-throwing-exceptions |
test/clang-tidy/google-objc-avoid-throwing-exception.m | ||
11 ↗ | (On Diff #122953) | nit: remove the empty line. |
clang-tidy/google/AvoidThrowingObjcExceptionCheck.cpp | ||
---|---|---|
22 ↗ | (On Diff #122953) | It looks like we also need to catch (no pun intended :) people who invoke the following class methods: +[NSException raise:format:] +[NSException raise:format:arguments:] We actually have a few places that use these: http://google3/googlemac/iPhone/Maps/SDK/Maps/GMSServices.mm?l=412&rcl=174266104 |
30 ↗ | (On Diff #122953) | Suggested wording: "Pass in NSError ** instead of using @throw to indicate Objective-C errors" |
clang-tidy/google/AvoidThrowingObjcExceptionCheck.h | ||
1 ↗ | (On Diff #122953) | Naming nit-pick: We are currently using ObjC, not Objc. |
21–22 ↗ | (On Diff #122953) | Grammar nit: according to Google Objective-C Style Guide -> according to the Google Objective-C Style Guide |
26 ↗ | (On Diff #122953) | Objc -> ObjC |
docs/clang-tidy/checks/google-objc-avoid-throwing-exception.rst | ||
6–7 ↗ | (On Diff #122953) | How about: This check finds @throw usages in Objective-C files. For the same reasons as the Google C++ style guide, we prefer not throwing exceptions from Objective-C code. Instead, prefer passing in NSError ** and return BOOL to indicate success or failure. A counterexample: - (void)readFile { if ([self isError]) { @throw [NSException exceptionWithName:...]; } } Instead, returning an error via NSError ** is preferred: - (BOOL)readFileWithError:(NSError **)error { if ([self isError]) { *error = [NSError errorWithDomain:...]; return NO; } return YES; } |
10 ↗ | (On Diff #122953) | +1 |
Looking good. Please rename the files correctly, or it will not build on Linux.
clang-tidy/google/AvoidThrowingObjcExceptionCheck.cpp | ||
---|---|---|
23 ↗ | (On Diff #123079) | Also need to match raise:format:arguments:. |
clang-tidy/google/AvoidThrowingObjcExceptionCheck.h | ||
1 ↗ | (On Diff #123079) | Don't forget to rename the file to AvoidThrowingObjcExceptionCheck.h. |
clang-tidy/google/CMakeLists.txt | ||
5 ↗ | (On Diff #123079) | Rename to AvoidThrowingObjCExceptionCheck.cpp |
clang-tidy/google/GoogleTidyModule.cpp | ||
18 ↗ | (On Diff #123079) | I think you renamed the #include but not the file.. should test on Linux, it won't work with a case-sensitive file system ;) |
docs/ReleaseNotes.rst | ||
63 ↗ | (On Diff #123079) | detect usage of @throw -> detect throwing exceptions |
docs/clang-tidy/checks/google-objc-avoid-throwing-exception.rst | ||
6 ↗ | (On Diff #123079) | Can you include the link to the C++ style guide section here instead of at the end? http://google.github.io/styleguide/cppguide.html#Exceptions |
6 ↗ | (On Diff #123079) | finds @throw usages -> finds uses of throwing exceptions |
clang-tidy/google/AvoidThrowingObjcExceptionCheck.h | ||
---|---|---|
1 ↗ | (On Diff #123079) | Oops! I mean, rename the file to AvoidThrowingObjCExceptionCheck.h. :) |
test/clang-tidy/google-objc-avoid-throwing-exception.m | ||
17–20 ↗ | (On Diff #123079) | Would be nice to have a test that confirms sending -raise:format: to a different object does not hit the warning. |
File names look good now, thanks.
clang-tidy/google/AvoidThrowingObjCExceptionCheck.cpp | ||
---|---|---|
1 ↗ | (On Diff #123091) | Objc -> ObjC |