11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 namespace cppcoreguidelines {
20 void ProTypeStaticCastDowncastCheck::registerMatchers(MatchFinder *Finder) {
21 if (!getLangOpts().CPlusPlus)
25 cxxStaticCastExpr(unless(isInTemplateInstantiation())).bind(
"cast"),
29 void ProTypeStaticCastDowncastCheck::check(
30 const MatchFinder::MatchResult &
Result) {
31 const auto *MatchedCast = Result.Nodes.getNodeAs<CXXStaticCastExpr>(
"cast");
32 if (MatchedCast->getCastKind() != CK_BaseToDerived)
35 QualType SourceType = MatchedCast->getSubExpr()->getType();
36 const auto *SourceDecl = SourceType->getPointeeCXXRecordDecl();
38 SourceDecl = SourceType->getAsCXXRecordDecl();
42 if (SourceDecl->isPolymorphic())
43 diag(MatchedCast->getOperatorLoc(),
44 "do not use static_cast to downcast from a base to a derived class; " 45 "use dynamic_cast instead")
46 << FixItHint::CreateReplacement(MatchedCast->getOperatorLoc(),
49 diag(MatchedCast->getOperatorLoc(),
50 "do not use static_cast to downcast from a base to a derived class");
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//