[clang-tools-extra] [clang-tidy] Fix readability-identifier-naming FP with DefaultCase on function templates (PR #189788)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 1 00:00:49 PDT 2026
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/189788
>From 945d2ecf2782ebed920265ae7aaefab3830aae25 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Wed, 1 Apr 2026 10:48:47 +0800
Subject: [PATCH 1/2] [clang-tidy] Fix readability-identifier-naming FP with
DefaultCase on function templates
---
.../utils/RenamerClangTidyCheck.cpp | 4 +++
clang-tools-extra/docs/ReleaseNotes.rst | 9 ++++--
...ntifier-naming-template-method-default.cpp | 30 +++++++++++++++++++
3 files changed, 41 insertions(+), 2 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-template-method-default.cpp
diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
index 0e4a6f49116da..7291bf121f1af 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
@@ -116,6 +116,10 @@ static bool hasNoName(const NamedDecl *Decl) {
}
static const NamedDecl *getFailureForNamedDecl(const NamedDecl *ND) {
+ if (const auto *Template = dyn_cast<FunctionTemplateDecl>(ND))
+ if (const FunctionDecl *TemplatedDecl = Template->getTemplatedDecl())
+ ND = TemplatedDecl;
+
const auto *Canonical = cast<NamedDecl>(ND->getCanonicalDecl());
if (Canonical != ND)
return Canonical;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 69dc5b9633398..5830adf1ebcc7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -217,7 +217,7 @@ Changes in existing checks
C++ files because suggested ``reinterpret_cast`` is not available in pure C.
- Improved :doc:`bugprone-derived-method-shadowing-base-method
- <clang-tidy/checks/bugprone/derived-method-shadowing-base-method>` check by
+ <clang-tidy/checks/bugprone/derived-method-shadowing-base-method>` check by
correctly ignoring function templates.
- Improved :doc:`bugprone-exception-escape
@@ -248,7 +248,7 @@ Changes in existing checks
<clang-tidy/checks/bugprone/std-namespace-modification>` check by fixing
false positives when extending the standard library with a specialization of
user-defined type and by removing detection of the compiler generated ``std``
- namespace extensions.
+ namespace extensions.
- Improved :doc:`bugprone-string-constructor
<clang-tidy/checks/bugprone/string-constructor>` check to detect suspicious
@@ -422,6 +422,11 @@ Changes in existing checks
now uses separate note diagnostics for each uninitialized enumerator, making
it easier to see which specific enumerators need explicit initialization.
+- Improved :doc:`readability-identifier-naming
+ <clang-tidy/checks/readability/identifier-naming>` check by fixing a false
+ positive where function templates could be diagnosed as generic identifiers
+ when ``DefaultCase`` was enabled.
+
- Improved :doc:`readability-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check:
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-template-method-default.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-template-method-default.cpp
new file mode 100644
index 0000000000000..d094e3ac71728
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-template-method-default.cpp
@@ -0,0 +1,30 @@
+// RUN: %check_clang_tidy %s readability-identifier-naming %t -std=c++20-or-later \
+// RUN: --config='{CheckOptions: { \
+// RUN: readability-identifier-naming.DefaultCase: lower_case, \
+// RUN: readability-identifier-naming.ClassCase: CamelCase, \
+// RUN: readability-identifier-naming.FunctionCase: camelBack, \
+// RUN: readability-identifier-naming.MethodCase: camelBack, \
+// RUN: }}'
+
+class Foo {
+public:
+ template <typename t>
+ void doStuff() {}
+
+ template <typename t>
+ void DoStuff() {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for method 'DoStuff' [readability-identifier-naming]
+ // CHECK-FIXES: void doStuff() {}
+};
+
+template <typename t>
+void freeFunction() {}
+
+template <typename t>
+void FreeFunction() {}
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: invalid case style for function 'FreeFunction' [readability-identifier-naming]
+// CHECK-FIXES: void freeFunction() {}
+
+int BadGlobal = 0;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for identifier 'BadGlobal' [readability-identifier-naming]
+// CHECK-FIXES: int bad_global = 0;
>From 1e1693a25820fc420e0b9bcedd0b7b2015733b49 Mon Sep 17 00:00:00 2001
From: Zeyi Xu <mitchell.xu2 at gmail.com>
Date: Wed, 1 Apr 2026 15:00:41 +0800
Subject: [PATCH 2/2] Update clang-tools-extra/docs/ReleaseNotes.rst
Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
---
clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 5830adf1ebcc7..83c1a982ee00d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -425,7 +425,7 @@ Changes in existing checks
- Improved :doc:`readability-identifier-naming
<clang-tidy/checks/readability/identifier-naming>` check by fixing a false
positive where function templates could be diagnosed as generic identifiers
- when ``DefaultCase`` was enabled.
+ when `DefaultCase` was enabled.
- Improved :doc:`readability-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check:
More information about the cfe-commits
mailing list