33 const MatchFinder::MatchResult &Result) {
34 const auto *MatchedCast = Result.Nodes.getNodeAs<CXXStaticCastExpr>(
"cast");
36 QualType SourceType = MatchedCast->getSubExpr()->getType();
37 const auto *SourceDecl = SourceType->getPointeeCXXRecordDecl();
39 SourceDecl = SourceType->getAsCXXRecordDecl();
43 if (SourceDecl->isPolymorphic()) {
44 diag(MatchedCast->getOperatorLoc(),
45 "do not use static_cast to downcast from a base to a derived class; "
46 "use dynamic_cast instead")
47 << FixItHint::CreateReplacement(MatchedCast->getOperatorLoc(),
55 diag(MatchedCast->getOperatorLoc(),
56 "do not use static_cast to downcast from a base to a derived class");
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.