| 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/ASTMatchers/ASTMatchFinder.h" |
| 11 | |
| 12 | using namespace clang::ast_matchers; |
| 13 | |
| 14 | namespace clang::tidy::linuxkernel { |
| 15 | |
| 16 | void 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 | NodeMatch: callExpr(callee(InnerMatcher: ErrFn), hasParent(NonCheckingStmts)).bind(ID: "call" ), Action: this); |
| 23 | |
| 24 | auto ReturnToCheck = returnStmt(hasReturnValue(InnerMatcher: callExpr(callee(InnerMatcher: ErrFn)))); |
| 25 | auto ReturnsErrFn = functionDecl(hasDescendant(ReturnToCheck)); |
| 26 | Finder->addMatcher(NodeMatch: callExpr(callee(InnerMatcher: ReturnsErrFn), hasParent(NonCheckingStmts)) |
| 27 | .bind(ID: "transitive_call" ), |
| 28 | Action: this); |
| 29 | } |
| 30 | |
| 31 | void MustCheckErrsCheck::check(const MatchFinder::MatchResult &Result) { |
| 32 | const auto *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>(ID: "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>(ID: "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 |
| 48 | |