RefCountedThisInsideConstructorChecker.cpp (1144B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "RefCountedThisInsideConstructorChecker.h" 6 #include "CustomMatchers.h" 7 8 void RefCountedThisInsideConstructorChecker::registerMatchers( 9 MatchFinder *AstMatcher) { 10 AstMatcher->addMatcher( 11 cxxConstructExpr(hasType(isSmartPtrToRefCounted()), 12 hasArgument(0, cxxThisExpr()), 13 hasAncestor(cxxConstructorDecl().bind("constructor"))) 14 .bind("call"), 15 this); 16 } 17 18 void RefCountedThisInsideConstructorChecker::check( 19 const MatchFinder::MatchResult &Result) { 20 const CXXConstructExpr *Call = 21 Result.Nodes.getNodeAs<CXXConstructExpr>("call"); 22 23 diag(Call->getBeginLoc(), 24 "Refcounting `this` inside the constructor is a footgun, `this` may be " 25 "destructed at the end of the constructor unless there's another strong " 26 "reference. Consider adding a separate Create function and do the work " 27 "there.", 28 DiagnosticIDs::Error); 29 }