[clang-tidy] Add a check for [super self] in initializers 🔍
authorStephane Moore <mog@google.com>
Wed, 17 Apr 2019 22:29:06 +0000 (22:29 +0000)
committerStephane Moore <mog@google.com>
Wed, 17 Apr 2019 22:29:06 +0000 (22:29 +0000)
commitb0c1f8c09e432e064951f6dac1ecac17a7050c37
tree21c3cff82315404fa845acd07751fdf4108cede1
parentcfd25a4b0e57ff27ec8a4b591b197066dba1f79d
[clang-tidy] Add a check for [super self] in initializers 🔍

Summary:
This check aims to address a relatively common benign error where
Objective-C subclass initializers call -self on their superclass instead
of invoking a superclass initializer, typically -init. The error is
typically benign because libobjc recognizes that improper initializer
chaining is common¹.

One theory for the frequency of this error might be that -init and -self
have the same return type which could potentially cause inappropriate
autocompletion to -self instead of -init. The equal selector lengths and
triviality of common initializer code probably contribute to errors like
this slipping through code review undetected.

This check aims to flag errors of this form in the interests of
correctness and reduce incidence of initialization failing to chain to
-[NSObject init].

[1] "In practice, it will be hard to rely on this function.
     Many classes do not properly chain -init calls."
From  _objc_rootInit in https://opensource.apple.com/source/objc4/objc4-750.1/runtime/NSObject.mm.auto.html.

Test Notes:
Verified via `make check-clang-tools`.

Subscribers: mgorny, xazax.hun, jdoerfert, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D59806

llvm-svn: 358620
clang-tools-extra/clang-tidy/objc/CMakeLists.txt
clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp
clang-tools-extra/clang-tidy/objc/SuperSelfCheck.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/objc/SuperSelfCheck.h [new file with mode: 0644]
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
clang-tools-extra/docs/clang-tidy/checks/objc-super-self.rst [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/objc-super-self.m [new file with mode: 0644]