10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
19 functionDecl(hasAnyName(
"ERR_PTR",
"PTR_ERR",
"IS_ERR",
"IS_ERR_OR_NULL",
20 "ERR_CAST",
"PTR_ERR_OR_ZERO"));
21 auto NonCheckingStmts = stmt(anyOf(compoundStmt(), labelStmt()));
23 callExpr(callee(ErrFn), hasParent(NonCheckingStmts)).bind(
"call"),
26 auto ReturnToCheck = returnStmt(hasReturnValue(callExpr(callee(ErrFn))));
27 auto ReturnsErrFn = functionDecl(hasDescendant(ReturnToCheck));
28 Finder->addMatcher(callExpr(callee(ReturnsErrFn), hasParent(NonCheckingStmts))
29 .bind(
"transitive_call"),
34 const auto *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>(
"call");
35 if (MatchedCallExpr) {
36 diag(MatchedCallExpr->getExprLoc(),
"result from function %0 is unused")
37 << MatchedCallExpr->getDirectCallee();
40 const auto *MatchedTransitiveCallExpr =
41 Result.Nodes.getNodeAs<CallExpr>(
"transitive_call");
42 if (MatchedTransitiveCallExpr) {
43 diag(MatchedTransitiveCallExpr->getExprLoc(),
44 "result from function %0 is unused but represents an error value")
45 << MatchedTransitiveCallExpr->getDirectCallee();
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.