10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
19 cxxStaticCastExpr(unless(isInTemplateInstantiation())).bind(
"cast"),
24 const MatchFinder::MatchResult &Result) {
25 const auto *MatchedCast = Result.Nodes.getNodeAs<CXXStaticCastExpr>(
"cast");
26 if (MatchedCast->getCastKind() != CK_BaseToDerived)
29 QualType SourceType = MatchedCast->getSubExpr()->getType();
30 const auto *SourceDecl = SourceType->getPointeeCXXRecordDecl();
32 SourceDecl = SourceType->getAsCXXRecordDecl();
36 if (SourceDecl->isPolymorphic())
37 diag(MatchedCast->getOperatorLoc(),
38 "do not use static_cast to downcast from a base to a derived class; "
39 "use dynamic_cast instead")
40 << FixItHint::CreateReplacement(MatchedCast->getOperatorLoc(),
43 diag(MatchedCast->getOperatorLoc(),
44 "do not use static_cast to downcast from a base to a derived class");
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.