1//===--- MustCheckErrsCheck.cpp - clang-tidy ------------------------------===//
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
9#include "MustCheckErrsCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::linuxkernel {
16
17void MustCheckErrsCheck::registerMatchers(MatchFinder *Finder) {
18 auto ErrFn =
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()));
22 Finder->addMatcher(
23 NodeMatch: callExpr(callee(InnerMatcher: ErrFn), hasParent(NonCheckingStmts)).bind(ID: "call"),
24 Action: this);
25
26 auto ReturnToCheck = returnStmt(hasReturnValue(InnerMatcher: callExpr(callee(InnerMatcher: ErrFn))));
27 auto ReturnsErrFn = functionDecl(hasDescendant(ReturnToCheck));
28 Finder->addMatcher(NodeMatch: callExpr(callee(InnerMatcher: ReturnsErrFn), hasParent(NonCheckingStmts))
29 .bind(ID: "transitive_call"),
30 Action: this);
31}
32
33void MustCheckErrsCheck::check(const MatchFinder::MatchResult &Result) {
34 const auto *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>(ID: "call");
35 if (MatchedCallExpr) {
36 diag(MatchedCallExpr->getExprLoc(), "result from function %0 is unused")
37 << MatchedCallExpr->getDirectCallee();
38 }
39
40 const auto *MatchedTransitiveCallExpr =
41 Result.Nodes.getNodeAs<CallExpr>(ID: "transitive_call");
42 if (MatchedTransitiveCallExpr) {
43 diag(MatchedTransitiveCallExpr->getExprLoc(),
44 "result from function %0 is unused but represents an error value")
45 << MatchedTransitiveCallExpr->getDirectCallee();
46 }
47}
48
49} // namespace clang::tidy::linuxkernel
50

source code of clang-tools-extra/clang-tidy/linuxkernel/MustCheckErrsCheck.cpp