[clang-tidy] Add new modernize use unary assert check
Patch by: Lilla Barancsuk
Differential Revision: https://reviews.llvm.org/D35257
llvm-svn: 307791
diff --git a/clang-tools-extra/clang-tidy/modernize/UnaryStaticAssertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
new file mode 100644
index 0000000..c2129e3
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
@@ -0,0 +1,45 @@
+//===--- UnaryStaticAssertCheck.cpp - clang-tidy---------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "UnaryStaticAssertCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
+ if (!getLangOpts().CPlusPlus1z)
+ return;
+
+ Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
+}
+
+void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *MatchedDecl =
+ Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
+ const StringLiteral *AssertMessage = MatchedDecl->getMessage();
+
+ SourceLocation Loc = MatchedDecl->getLocation();
+
+ if (!AssertMessage || AssertMessage->getLength() ||
+ AssertMessage->getLocStart().isMacroID() || Loc.isMacroID())
+ return;
+
+ diag(Loc,
+ "use unary 'static_assert' when the string literal is an empty string")
+ << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
+}
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang