[PATCH] D118010: [clang-tidy] Fix nested namespaces in `readability-static-definition-in-anonymous-namespace` check
Richard via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 26 21:08:55 PST 2022
This revision was automatically updated to reflect the committed changes.
Closed by commit rG836950c4e602: [clang-tidy] Fix nested namespaces in `readability-static-definition-in… (authored by Izaron, committed by LegalizeAdulthood).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D118010/new/
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.403491.patch
Type: text/x-patch
Size: 2715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220127/86957cf6/attachment-0001.bin>
More information about the cfe-commits
mailing list