24 MatchFinder *Finder) {
25 auto AnyCalleeName = [](ArrayRef<StringRef> CalleeName) {
26 return allOf(unless(isMacroID()), unless(cxxMemberCallExpr()),
27 callee(expr(ignoringImpCasts(
28 declRefExpr(to(namedDecl(hasAnyName(CalleeName))),
29 hasAnyTemplateArgumentLoc(anything()))
33 auto CondExpr = hasCondition(implicitCastExpr(
34 has(callExpr(AnyCalleeName({
"cast",
"dyn_cast"})).bind(
"cond"))));
36 auto CondExprOrCondVar =
37 anyOf(hasConditionVariableStatement(containsDeclaration(
38 0, varDecl(hasInitializer(callExpr(AnyCalleeName({
"cast"}))))
42 auto CallWithBindedArg =
45 {
"isa",
"cast",
"cast_or_null",
"dyn_cast",
"dyn_cast_or_null"}),
46 hasArgument(0, mapAnyOf(declRefExpr, cxxMemberCallExpr).bind(
"arg")))
49 Finder->addMatcher(ifStmt(CondExprOrCondVar),
this);
50 Finder->addMatcher(forStmt(CondExprOrCondVar),
this);
51 Finder->addMatcher(whileStmt(CondExprOrCondVar),
this);
52 Finder->addMatcher(doStmt(CondExpr),
this);
53 Finder->addMatcher(binaryOperator(hasRHS(ignoringImpCasts(CallWithBindedArg)),
54 hasLHS(implicitCastExpr().bind(
"lhs")),
55 hasOperatorName(
"&&"),
56 unless(isExpansionInFileMatching(
57 "llvm/include/llvm/Support/Casting.h")))
63 const MatchFinder::MatchResult &Result) {
64 const auto *Callee = Result.Nodes.getNodeAs<DeclRefExpr>(
"callee");
66 assert(Callee &&
"Callee should be binded if anything is matched");
72 const SourceLocation StartLoc = Callee->getLocation();
73 const SourceLocation EndLoc = Callee->getNameInfo().getEndLoc();
75 if (Result.Nodes.getNodeAs<VarDecl>(
"var")) {
77 "cast<> in conditional will assert rather than return a null pointer")
78 << FixItHint::CreateReplacement(SourceRange(StartLoc, EndLoc),
80 }
else if (Result.Nodes.getNodeAs<CallExpr>(
"cond")) {
82 "cast<> in conditional will assert rather than return a null pointer";
83 if (Callee->getDecl()->getName() ==
"dyn_cast")
84 Message =
"return value from dyn_cast<> not used";
86 diag(StartLoc, Message)
87 << FixItHint::CreateReplacement(SourceRange(StartLoc, EndLoc),
"isa");
88 }
else if (Result.Nodes.getNodeAs<BinaryOperator>(
"and")) {
89 const auto *LHS = Result.Nodes.getNodeAs<ImplicitCastExpr>(
"lhs");
90 const auto *RHS = Result.Nodes.getNodeAs<CallExpr>(
"rhs");
91 const auto *Arg = Result.Nodes.getNodeAs<Expr>(
"arg");
93 assert(LHS &&
"LHS is null");
94 assert(RHS &&
"RHS is null");
95 assert(Arg &&
"Arg is null");
97 auto GetText = [&](SourceRange R) {
98 return Lexer::getSourceText(CharSourceRange::getTokenRange(R),
99 *Result.SourceManager, getLangOpts());
102 const StringRef LHSString = GetText(LHS->getSourceRange());
103 const StringRef ArgString = GetText(Arg->getSourceRange());
105 if (ArgString != LHSString)
111 const std::string Replacement = llvm::formatv(
112 "{}isa_and_nonnull{}",
113 GetText(Callee->getQualifierLoc().getSourceRange()),
114 GetText(SourceRange(Callee->getLAngleLoc(), RHS->getEndLoc())));
116 diag(LHS->getBeginLoc(),
117 "isa_and_nonnull<> is preferred over an explicit test for null "
118 "followed by calling isa<>")
119 << FixItHint::CreateReplacement(
120 SourceRange(LHS->getBeginLoc(), RHS->getEndLoc()), Replacement);
123 R
"(One of "var", "cond" and "and" should be binded if anything is matched)");