[clang-tools-extra] 836950c - [clang-tidy] Fix nested namespaces in `readability-static-definition-in-anonymous-namespace` check
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 26 21:08:46 PST 2022
Author: Evgeny Shulgin
Date: 2022-01-26T21:54:17-07:00
New Revision: 836950c4e602548e47851b66a261472f124994fa
URL: https://github.com/llvm/llvm-project/commit/836950c4e602548e47851b66a261472f124994fa
DIFF: https://github.com/llvm/llvm-project/commit/836950c4e602548e47851b66a261472f124994fa.diff
LOG: [clang-tidy] Fix nested namespaces in `readability-static-definition-in-anonymous-namespace` check
The check previously inspected only the immediate parent namespace.
`static` in a named namespace within an unnamed namespace is still
redundant.
We will use `Decl::isInAnonymousNamespace()` method that traverses the
namespaces hierarchy recursively.
Differential Revision: https://reviews.llvm.org/D118010
Added:
Modified:
clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp
clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst
clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp b/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp
index ef973a1927b07..b76c1837b8019 100644
--- a/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp
@@ -17,12 +17,16 @@ namespace clang {
namespace tidy {
namespace readability {
+AST_MATCHER(NamedDecl, isInAnonymousNamespace) {
+ return Node.isInAnonymousNamespace();
+}
+
void StaticDefinitionInAnonymousNamespaceCheck::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(
namedDecl(anyOf(functionDecl(isDefinition(), isStaticStorageClass()),
varDecl(isDefinition(), isStaticStorageClass())),
- hasParent(namespaceDecl(isAnonymous())))
+ isInAnonymousNamespace())
.bind("static-def"),
this);
}
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst
index c1803d4b13052..91d40bd713ee2 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst
@@ -12,7 +12,10 @@ visibility of definitions to a single translation unit.
namespace {
static int a = 1; // Warning.
- static const b = 1; // Warning.
+ static const int b = 1; // Warning.
+ namespace inner {
+ static int c = 1; // Warning.
+ }
}
The check will apply a fix by removing the redundant ``static`` qualifier.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp
index 5c3c8c12d683c..e9938db4f5b83 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp
@@ -36,6 +36,21 @@ DEFINE_STATIC int h = 1;
DEFINE_STATIC_VAR(i);
// CHECK-FIXES: {{^}}DEFINE_STATIC_VAR(i);
+namespace inner {
+int a = 1;
+const int b = 1;
+static int c = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: 'c' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]
+// CHECK-FIXES: {{^}}int c = 1;
+namespace deep_inner {
+int a = 1;
+const int b = 1;
+static int c = 1;
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: 'c' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]
+// CHECK-FIXES: {{^}}int c = 1;
+} // namespace deep_inner
+} // namespace inner
+
} // namespace
namespace N {
More information about the cfe-commits
mailing list