[clang-tools-extra] 3137347 - [clang-tidy] Fix fix-it overlaps in readability-static-definition-in-anonymous-namespace (#86599)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 26 10:20:29 PDT 2024
Author: Piotr Zegar
Date: 2024-03-26T18:20:25+01:00
New Revision: 313734795c67ca0bfb82f96b39e123774ddf24b4
URL: https://github.com/llvm/llvm-project/commit/313734795c67ca0bfb82f96b39e123774ddf24b4
DIFF: https://github.com/llvm/llvm-project/commit/313734795c67ca0bfb82f96b39e123774ddf24b4.diff
LOG: [clang-tidy] Fix fix-it overlaps in readability-static-definition-in-anonymous-namespace (#86599)
Because check emitted multiple warnings for every template instance
fix-it couldn't be applied due to overlaps.
Using TK_IgnoreUnlessSpelledInSource and restricting check to C++ only.
Added:
Modified:
clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.h
clang-tools-extra/docs/ReleaseNotes.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.h b/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.h
index c28087e07e9b61..620cd6e3f2f877 100644
--- a/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.h
@@ -24,6 +24,12 @@ class StaticDefinitionInAnonymousNamespaceCheck : public ClangTidyCheck {
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+ return LangOpts.CPlusPlus;
+ }
+ std::optional<TraversalKind> getCheckTraversalKind() const override {
+ return TK_IgnoreUnlessSpelledInSource;
+ }
};
} // namespace clang::tidy::readability
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2392ccaf65754f..f6ae712510ca89 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -253,6 +253,11 @@ Changes in existing checks
mode by resolving symbolic links to header files. Fixed handling of Hungarian
Prefix when configured to `LowerCase`.
+- Improved :doc:`readability-static-definition-in-anonymous-namespace
+ <clang-tidy/checks/readability/static-definition-in-anonymous-namespace>`
+ check by resolving fix-it overlaps in template code by disregarding implicit
+ instances.
+
Removed checks
^^^^^^^^^^^^^^
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 e9938db4f5b83f..e204199393db42 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
@@ -51,6 +51,17 @@ static int c = 1;
} // namespace deep_inner
} // namespace inner
+template<typename T>
+static void printTemplate(T&&) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: 'printTemplate' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]
+// CHECK-FIXES: {{^}}void printTemplate(T&&) {}
+
+void testTemplate() {
+ printTemplate(5);
+ printTemplate(5U);
+ printTemplate("some string");
+}
+
} // namespace
namespace N {
More information about the cfe-commits
mailing list