1 //===--- MustCheckErrsCheck.cpp - clang-tidy ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MustCheckErrsCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
15 namespace clang::tidy::linuxkernel
{
17 void MustCheckErrsCheck::registerMatchers(MatchFinder
*Finder
) {
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"),
33 void MustCheckErrsCheck::check(const MatchFinder::MatchResult
&Result
) {
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();
49 } // namespace clang::tidy::linuxkernel