Such implementations may override the class's own implementation, and even be a danger in case someone later comes and adds one to the class itself. Most times this has been encountered have been a mistake.
Details
Diff Detail
Event Timeline
clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-categories.rst | ||
---|---|---|
11 | Need an end quote on dealloc |
clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.cpp | ||
---|---|---|
21 | Add isInstanceMethod() within the objcMethodDecl? | |
clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.h | ||
24 | What do you think of the name DeallocInCategoryCheck? | |
clang-tools-extra/test/clang-tidy/checkers/objc-dealloc-in-categories.m | ||
23 | Please add a class method +dealloc and make sure that the check does not trigger on that. | |
35 | Perhaps add an @implementation for Baz (not a category implementation) that contains -dealloc to ensure that a declaration in a category with a definition in the implementation does not trigger? |
clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.cpp | ||
---|---|---|
19 | Early return if the language is not an Objective-C variant: // This check should only be applied to Objective-C sources. if (!getLangOpts().ObjC) return; | |
21 | Technically, isn't -dealloc specific to certain classes, e.g., [NSObject](https://developer.apple.com/documentation/objectivec/nsobject/1571947-dealloc?language=objc)) and NSProxy? If that is true, we should technically check that the category is on a class that is or is derived from a relevant class like NSObject or NSProxy. |
Updated the diff based on review feedback.
clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.cpp | ||
---|---|---|
21 | Technically true, but given the prevalence of extant ObjC documentation talking about the -dealloc method in terms of deallocation, it seems highly unlikely any non-NSObject/NSProxy-rooted class hierarchies are going to use -dealloc in any other context, and want to allow implementations in a category. | |
clang-tools-extra/clang-tidy/objc/DeallocInCategoriesCheck.h | ||
24 | Much better. |
Looks in good shape ๐ A couple nits and polish ideas.
clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.cpp | ||
---|---|---|
34 โ | (On Diff #240716) | What do you think of making a fixit that deletes the -dealloc implementation? |
36 โ | (On Diff #240716) | I can't speak authoritatively regarding project convention but I believe that it's rare to condition on a nonnull match when matching on a single AST node (in other words, check would not have been called if MatchedDecl were null). I believe that convention is either to omit the expected condition or to assert the expected condition. Examples: |
37โ38 โ | (On Diff #240716) | What do you think of binding the category implementation declaration and including that in the diagnostic message? Finder->addMatcher(objcMethodDecl(isInstanceMethod(), hasName("dealloc"), hasDeclContext(objcCategoryImplDecl().bind("impl"))) .bind("dealloc"), this); // ... const auto *CID = Result.Nodes.getNodeAs<ObjCCategoryImplDecl>("impl"); diag(MatchedDecl->getLocation(), "category %0 should not implement -dealloc") << CID; (admittedly, I don't recall off the top of my head how ObjCCategoryImplDecl renders in diagnostic messages so some additional tuning could be warranted) |
clang-tools-extra/docs/clang-tidy/checks/objc-dealloc-in-category.rst | ||
7 โ | (On Diff #240716) | `-dealloc` |
10โ11 โ | (On Diff #240716) | Replace "just before an object is deallocated" with "to deallocate an object". "Deallocates the memory occupied by the receiver." |
11 โ | (On Diff #240716) | Replace ", but if" with ". If"? |
11 โ | (On Diff #240716) | I think we should add a comma after `-dealloc`? |
12โ13 โ | (On Diff #240716) | Isn't it a bit more nefarious than that? -dealloc in the main implementation becomes inaccessible (though the superclass can still be invoked). Perhaps it would suffice to say that "unexpected deallocation behavior may occur"? |
Implemented review feedback.
Updated diagnostic message to mention the category name.
clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.cpp | ||
---|---|---|
34 โ | (On Diff #240716) | I would assume any non-empty -dealloc has some code the developer would want to migrate elsewhere rather than just delete, so I would prefer not to offer to outright delete it. Might be good for empty -dealloc methods or ones that just call super (pre-ARC, bah humbug!)โbut I'd prefer to take that up in a follow-up change. |
37โ38 โ | (On Diff #240716) | I think it's a great idea! ๐ Done! |
One last correction and I think that you're all set!
clang-tools-extra/docs/clang-tidy/checks/list.rst | ||
---|---|---|
282 | Please revert this line. |
LGTM, just one nit-pick.
clang-tools-extra/clang-tidy/objc/DeallocInCategoryCheck.h | ||
---|---|---|
24 โ | (On Diff #242371) | class DeallocInCategoryCheck final |
What do you think of the name DeallocInCategoryCheck?