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")))
50 stmt(anyOf(ifStmt(CondExprOrCondVar), forStmt(CondExprOrCondVar),
51 whileStmt(CondExprOrCondVar), doStmt(CondExpr),
52 binaryOperator(unless(isExpansionInFileMatching(
53 "llvm/include/llvm/Support/Casting.h")),
54 hasOperatorName(
"&&"),
55 hasLHS(implicitCastExpr().bind(
"lhs")),
56 hasRHS(ignoringImpCasts(CallWithBindedArg)))
62 const MatchFinder::MatchResult &Result) {
63 const auto *Callee = Result.Nodes.getNodeAs<DeclRefExpr>(
"callee");
65 assert(Callee &&
"Callee should be binded if anything is matched");
71 SourceLocation StartLoc = Callee->getLocation();
72 SourceLocation EndLoc = Callee->getNameInfo().getEndLoc();
74 if (Result.Nodes.getNodeAs<VarDecl>(
"var")) {
76 "cast<> in conditional will assert rather than return a null pointer")
77 << FixItHint::CreateReplacement(SourceRange(StartLoc, EndLoc),
79 }
else if (Result.Nodes.getNodeAs<CallExpr>(
"cond")) {
81 "cast<> in conditional will assert rather than return a null pointer";
82 if (Callee->getDecl()->getName() ==
"dyn_cast")
83 Message =
"return value from dyn_cast<> not used";
85 diag(StartLoc, Message)
86 << FixItHint::CreateReplacement(SourceRange(StartLoc, EndLoc),
"isa");
87 }
else if (Result.Nodes.getNodeAs<BinaryOperator>(
"and")) {
88 const auto *LHS = Result.Nodes.getNodeAs<ImplicitCastExpr>(
"lhs");
89 const auto *RHS = Result.Nodes.getNodeAs<CallExpr>(
"rhs");
90 const auto *Arg = Result.Nodes.getNodeAs<Expr>(
"arg");
92 assert(LHS &&
"LHS is null");
93 assert(RHS &&
"RHS is null");
94 assert(Arg &&
"Arg is null");
96 auto GetText = [&](SourceRange R) {
97 return Lexer::getSourceText(CharSourceRange::getTokenRange(R),
98 *Result.SourceManager, getLangOpts());
101 const StringRef LHSString = GetText(LHS->getSourceRange());
102 const StringRef ArgString = GetText(Arg->getSourceRange());
104 if (ArgString != LHSString)
110 const std::string Replacement = llvm::formatv(
111 "{}isa_and_nonnull{}",
112 GetText(Callee->getQualifierLoc().getSourceRange()),
113 GetText(SourceRange(Callee->getLAngleLoc(), RHS->getEndLoc())));
115 diag(LHS->getBeginLoc(),
116 "isa_and_nonnull<> is preferred over an explicit test for null "
117 "followed by calling isa<>")
118 << FixItHint::CreateReplacement(
119 SourceRange(LHS->getBeginLoc(), RHS->getEndLoc()), Replacement);
122 R
"(One of "var", "cond" and "and" should be binded if anything is matched)");