[PATCH] D118010: [clang-tidy] Fix nested namespaces in `readability-static-definition-in-anonymous-namespace` check
Evgeny Shulgin via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 23 14:55:30 PST 2022
Izaron created this revision.
Izaron added reviewers: hokein, alexfh.
Herald added subscribers: carlosgalvezp, xazax.hun.
Izaron requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D118010
Files:
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
Index: clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-static-definition-in-anonymous-namespace.cpp
@@ -36,6 +36,21 @@
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 {
Index: clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst
+++ clang-tools-extra/docs/clang-tidy/checks/readability-static-definition-in-anonymous-namespace.rst
@@ -12,7 +12,10 @@
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.
Index: clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp
@@ -17,12 +17,16 @@
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);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118010.402378.patch
Type: text/x-patch
Size: 2715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220123/8d90b951/attachment.bin>
More information about the cfe-commits
mailing list