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