ExplicitOperatorBoolChecker.cpp (1187B)
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 "ExplicitOperatorBoolChecker.h" 6 #include "CustomMatchers.h" 7 8 void ExplicitOperatorBoolChecker::registerMatchers(MatchFinder *AstMatcher) { 9 AstMatcher->addMatcher( 10 cxxMethodDecl(allOf(isFirstParty(), hasName("operator bool"))) 11 .bind("node"), 12 this); 13 } 14 15 void ExplicitOperatorBoolChecker::check( 16 const MatchFinder::MatchResult &Result) { 17 const CXXConversionDecl *Method = 18 Result.Nodes.getNodeAs<CXXConversionDecl>("node"); 19 const CXXRecordDecl *Clazz = Method->getParent(); 20 21 if (!Method->isExplicit() && !hasCustomAttribute<moz_implicit>(Method) && 22 !ASTIsInSystemHeader(Method->getASTContext(), *Method) && 23 isInterestingDeclForImplicitConversion(Method)) { 24 diag(Method->getBeginLoc(), "bad implicit conversion operator for %0", 25 DiagnosticIDs::Error) 26 << Clazz; 27 diag(Method->getBeginLoc(), "consider adding the explicit keyword to %0", 28 DiagnosticIDs::Note) 29 << "'operator bool'"; 30 } 31 }