AvoidNSErrorInitCheck.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. //===--- AvoidNSErrorInitCheck.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 "AvoidNSErrorInitCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::objc {
  13. void AvoidNSErrorInitCheck::registerMatchers(MatchFinder *Finder) {
  14. Finder->addMatcher(objcMessageExpr(hasSelector("init"),
  15. hasReceiverType(asString("NSError *")))
  16. .bind("nserrorInit"),
  17. this);
  18. }
  19. void AvoidNSErrorInitCheck::check(const MatchFinder::MatchResult &Result) {
  20. const auto *MatchedExpr =
  21. Result.Nodes.getNodeAs<ObjCMessageExpr>("nserrorInit");
  22. diag(MatchedExpr->getBeginLoc(),
  23. "use errorWithDomain:code:userInfo: or initWithDomain:code:userInfo: to "
  24. "create a new NSError");
  25. }
  26. } // namespace clang::tidy::objc