[clang-tools-extra] [clang-tidy] Fix readability-identifier-naming FP with DefaultCase on function templates (PR #189788)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 31 19:49:32 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: Zeyi Xu (zeyi2)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/189788.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp (+4)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+7-2)
- (added) clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-template-method-default.cpp (+30)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/189788
More information about the cfe-commits
mailing list