diff --git a/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp b/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp +++ b/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); } 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 --- 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 @@ 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 --- 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_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 {