UnaryStaticAssertCheck.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //===--- UnaryStaticAssertCheck.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 "UnaryStaticAssertCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::modernize {
  13. void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
  14. Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
  15. }
  16. void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
  17. const auto *MatchedDecl =
  18. Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
  19. const StringLiteral *AssertMessage = MatchedDecl->getMessage();
  20. SourceLocation Loc = MatchedDecl->getLocation();
  21. if (!AssertMessage || AssertMessage->getLength() ||
  22. AssertMessage->getBeginLoc().isMacroID() || Loc.isMacroID())
  23. return;
  24. diag(Loc,
  25. "use unary 'static_assert' when the string literal is an empty string")
  26. << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
  27. }
  28. } // namespace clang::tidy::modernize