MustCheckErrsCheck.cpp 1.9 KB

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