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