clang-tools 22.0.0git
MustCheckErrsCheck.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/ASTMatchers/ASTMatchFinder.h"
11
12using namespace clang::ast_matchers;
13
15
16void MustCheckErrsCheck::registerMatchers(MatchFinder *Finder) {
17 auto ErrFn =
18 functionDecl(hasAnyName("ERR_PTR", "PTR_ERR", "IS_ERR", "IS_ERR_OR_NULL",
19 "ERR_CAST", "PTR_ERR_OR_ZERO"));
20 auto NonCheckingStmts = stmt(anyOf(compoundStmt(), labelStmt()));
21 Finder->addMatcher(
22 callExpr(callee(ErrFn), hasParent(NonCheckingStmts)).bind("call"), this);
23
24 auto ReturnToCheck = returnStmt(hasReturnValue(callExpr(callee(ErrFn))));
25 auto ReturnsErrFn = functionDecl(hasDescendant(ReturnToCheck));
26 Finder->addMatcher(callExpr(callee(ReturnsErrFn), hasParent(NonCheckingStmts))
27 .bind("transitive_call"),
28 this);
29}
30
31void MustCheckErrsCheck::check(const MatchFinder::MatchResult &Result) {
32 const auto *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>("call");
33 if (MatchedCallExpr) {
34 diag(MatchedCallExpr->getExprLoc(), "result from function %0 is unused")
35 << MatchedCallExpr->getDirectCallee();
36 }
37
38 const auto *MatchedTransitiveCallExpr =
39 Result.Nodes.getNodeAs<CallExpr>("transitive_call");
40 if (MatchedTransitiveCallExpr) {
41 diag(MatchedTransitiveCallExpr->getExprLoc(),
42 "result from function %0 is unused but represents an error value")
43 << MatchedTransitiveCallExpr->getDirectCallee();
44 }
45}
46
47} // namespace clang::tidy::linuxkernel
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override