Skip to content

Commit 3137347

Browse files
authored
[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.
1 parent f4fc959 commit 3137347

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class StaticDefinitionInAnonymousNamespaceCheck : public ClangTidyCheck {
2424
: ClangTidyCheck(Name, Context) {}
2525
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2626
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
27+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
28+
return LangOpts.CPlusPlus;
29+
}
30+
std::optional<TraversalKind> getCheckTraversalKind() const override {
31+
return TK_IgnoreUnlessSpelledInSource;
32+
}
2733
};
2834

2935
} // namespace clang::tidy::readability

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ Changes in existing checks
253253
mode by resolving symbolic links to header files. Fixed handling of Hungarian
254254
Prefix when configured to `LowerCase`.
255255

256+
- Improved :doc:`readability-static-definition-in-anonymous-namespace
257+
<clang-tidy/checks/readability/static-definition-in-anonymous-namespace>`
258+
check by resolving fix-it overlaps in template code by disregarding implicit
259+
instances.
260+
256261
Removed checks
257262
^^^^^^^^^^^^^^
258263

clang-tools-extra/test/clang-tidy/checkers/readability/static-definition-in-anonymous-namespace.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ static int c = 1;
5151
} // namespace deep_inner
5252
} // namespace inner
5353

54+
template<typename T>
55+
static void printTemplate(T&&) {}
56+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: 'printTemplate' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]
57+
// CHECK-FIXES: {{^}}void printTemplate(T&&) {}
58+
59+
void testTemplate() {
60+
printTemplate(5);
61+
printTemplate(5U);
62+
printTemplate("some string");
63+
}
64+
5465
} // namespace
5566

5667
namespace N {

0 commit comments

Comments
 (0)