clang-tools 23.0.0git
PreferIsaOrDynCastInConditionalsCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13#include "llvm/Support/FormatVariadic.h"
14
15using namespace clang::ast_matchers;
16
18
19namespace {
20AST_MATCHER(Expr, isMacroID) { return Node.getExprLoc().isMacroID(); }
21} // namespace
22
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()))
30 .bind("callee")))));
31 };
32
33 auto CondExpr = hasCondition(implicitCastExpr(
34 has(callExpr(AnyCalleeName({"cast", "dyn_cast"})).bind("cond"))));
35
36 auto CondExprOrCondVar =
37 anyOf(hasConditionVariableStatement(containsDeclaration(
38 0, varDecl(hasInitializer(callExpr(AnyCalleeName({"cast"}))))
39 .bind("var"))),
40 CondExpr);
41
42 auto CallWithBindedArg =
43 callExpr(
44 AnyCalleeName(
45 {"isa", "cast", "cast_or_null", "dyn_cast", "dyn_cast_or_null"}),
46 hasArgument(0, mapAnyOf(declRefExpr, cxxMemberCallExpr).bind("arg")))
47 .bind("rhs");
48
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")))
58 .bind("and"),
59 this);
60}
61
63 const MatchFinder::MatchResult &Result) {
64 const auto *Callee = Result.Nodes.getNodeAs<DeclRefExpr>("callee");
65
66 assert(Callee && "Callee should be binded if anything is matched");
67
68 // The first and last letter of the identifier
69 // llvm::cast<T>(x)
70 // ^ ^
71 // StartLoc EndLoc
72 const SourceLocation StartLoc = Callee->getLocation();
73 const SourceLocation EndLoc = Callee->getNameInfo().getEndLoc();
74
75 if (Result.Nodes.getNodeAs<VarDecl>("var")) {
76 diag(StartLoc,
77 "cast<> in conditional will assert rather than return a null pointer")
78 << FixItHint::CreateReplacement(SourceRange(StartLoc, EndLoc),
79 "dyn_cast");
80 } else if (Result.Nodes.getNodeAs<CallExpr>("cond")) {
81 StringRef Message =
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";
85
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");
92
93 assert(LHS && "LHS is null");
94 assert(RHS && "RHS is null");
95 assert(Arg && "Arg is null");
96
97 auto GetText = [&](SourceRange R) {
98 return Lexer::getSourceText(CharSourceRange::getTokenRange(R),
99 *Result.SourceManager, getLangOpts());
100 };
101
102 const StringRef LHSString = GetText(LHS->getSourceRange());
103 const StringRef ArgString = GetText(Arg->getSourceRange());
104
105 if (ArgString != LHSString)
106 return;
107
108 // It is not clear which is preferred between `isa_and_nonnull` and
109 // `isa_and_present`. See
110 // https://discourse.llvm.org/t/psa-swapping-out-or-null-with-if-present/65018
111 const std::string Replacement = llvm::formatv(
112 "{}isa_and_nonnull{}",
113 GetText(Callee->getQualifierLoc().getSourceRange()),
114 GetText(SourceRange(Callee->getLAngleLoc(), RHS->getEndLoc())));
115
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);
121 } else {
122 llvm_unreachable(
123 R"(One of "var", "cond" and "and" should be binded if anything is matched)");
124 }
125}
126
127} // namespace clang::tidy::llvm_check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)