[clang-tools-extra] [clang-tidy] Fix readability-identifier-naming FP on function templates (PR #198517)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 20 04:07:47 PDT 2026
https://github.com/CarvedCoder updated https://github.com/llvm/llvm-project/pull/198517
>From f41ac8fa6ff81f875c096202dbbbf5043d9acecd Mon Sep 17 00:00:00 2001
From: CarvedCoder <shreyansbehera84 at gmail.com>
Date: Tue, 19 May 2026 19:10:26 +0530
Subject: [PATCH 1/4] [clang-tidy] Fix readability-identifier-naming on forward
declarations
---
.../readability/IdentifierNamingCheck.cpp | 19 +++++
.../identifier-naming-forward-decl.cpp | 70 +++++++++++++++++++
.../readability/identifier-naming.cpp | 4 +-
3 files changed, 91 insertions(+), 2 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index a356e1390b62d..5db0ae4c9bec1 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -11,6 +11,7 @@
#include "../GlobList.h"
#include "../utils/ASTUtils.h"
#include "clang/AST/CXXInheritance.h"
+#include "clang/AST/DeclTemplate.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/ArrayRef.h"
@@ -1189,6 +1190,24 @@ StyleKind IdentifierNamingCheck::findStyleKind(
return SK_Enum;
}
+ if (Decl->isStruct() && NamingStyles[SK_Struct])
+ return SK_Struct;
+
+ if (Decl->isStruct() && NamingStyles[SK_Class])
+ return SK_Class;
+
+ if (Decl->isClass() && NamingStyles[SK_Class])
+ return SK_Class;
+
+ if (Decl->isClass() && NamingStyles[SK_Struct])
+ return SK_Struct;
+
+ if (Decl->isUnion() && NamingStyles[SK_Union])
+ return SK_Union;
+
+ if (Decl->isEnum() && NamingStyles[SK_Enum])
+ return SK_Enum;
+
return undefinedStyle(NamingStyles);
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp
new file mode 100644
index 0000000000000..5011abfac78e7
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp
@@ -0,0 +1,70 @@
+// RUN: %check_clang_tidy -std=c++20 %s readability-identifier-naming %t -- \
+// RUN: -config='{CheckOptions: { \
+// RUN: readability-identifier-naming.DefaultCase: lower_case, \
+// RUN: readability-identifier-naming.ClassCase: CamelCase, \
+// RUN: readability-identifier-naming.StructCase: CamelCase, \
+// RUN: readability-identifier-naming.UnionCase: CamelCase, \
+// RUN: readability-identifier-naming.TemplateParameterCase: CamelCase \
+// RUN: }}'
+
+// Forward declarations should use their semantic declaration kind
+// instead of falling back to DefaultCase.
+
+// Namespace-scope forward declarations.
+
+class GoodClass;
+
+class bad_class;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'bad_class'
+
+struct GoodStruct;
+
+struct bad_struct;
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'bad_struct'
+
+union GoodUnion;
+
+union bad_union;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for union 'bad_union'
+
+// Nested forward declarations.
+
+class Outer {
+ class GoodInnerClass;
+
+ class bad_inner_class;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for class 'bad_inner_class'
+
+ struct GoodInnerStruct;
+
+ struct bad_inner_struct;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for struct 'bad_inner_struct'
+
+ union GoodInnerUnion;
+
+ union bad_inner_union;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for union 'bad_inner_union'
+};
+
+// Template forward declarations.
+
+template <typename T>
+class GoodTemplateClass;
+
+template <typename T>
+class bad_template_class;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'bad_template_class'
+
+template <typename T>
+struct GoodTemplateStruct;
+
+template <typename T>
+struct bad_template_struct;
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'bad_template_struct'
+
+template <typename T>
+union GoodTemplateUnion;
+
+template <typename T>
+union bad_template_union;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for union 'bad_template_union'
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
index f0a72ee31bc21..ef02f0f8a200b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
@@ -248,8 +248,8 @@ class my_class;
// Just make sure the fix is applied.
// CHECK-FIXES: class CMyClass;
-class my_forward_declared_class; // No warning should be triggered.
-
+class my_forward_declared_class;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'my_forward_declared_class'
const int my_class::classConstant = 4;
// CHECK-FIXES: const int CMyClass::kClassConstant = 4;
>From 2e9aa15cf2d7532a3a016ed8fa2777264f2b708f Mon Sep 17 00:00:00 2001
From: CarvedCoder <shreyansbehera84 at gmail.com>
Date: Wed, 20 May 2026 16:15:45 +0530
Subject: [PATCH 2/4] Added release notes
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 9f601babf7b27..2c15c02aceb6e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -676,6 +676,10 @@ Changes in existing checks
- Fixed a false positive where function templates could be diagnosed as generic
identifiers when `DefaultCase` was enabled.
+ - Fixed forward declarations incorrectly falling back to `DefaultCase`
+ instead of using their semantic declaration kind (for example
+ `ClassCase`, `StructCase`, or `UnionCase`).
+
- Improved :doc:`readability-implicit-bool-conversion
<clang-tidy/checks/readability/implicit-bool-conversion>` check:
>From 85fa26fa984a23a5c93b6f2573d786eeda3a8352 Mon Sep 17 00:00:00 2001
From: CarvedCoder <shreyansbehera84 at gmail.com>
Date: Wed, 20 May 2026 16:34:42 +0530
Subject: [PATCH 3/4] added fix-its
---
.../readability/identifier-naming-forward-decl.cpp | 10 ++++++++++
.../checkers/readability/identifier-naming.cpp | 2 ++
2 files changed, 12 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp
index 5011abfac78e7..23b2ed9af4176 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-forward-decl.cpp
@@ -16,16 +16,19 @@ class GoodClass;
class bad_class;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'bad_class'
+// CHECK-FIXES: class BadClass;
struct GoodStruct;
struct bad_struct;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'bad_struct'
+// CHECK-FIXES: class BadStruct;
union GoodUnion;
union bad_union;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for union 'bad_union'
+// CHECK-FIXES: class BadUnion;
// Nested forward declarations.
@@ -34,16 +37,20 @@ class Outer {
class bad_inner_class;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for class 'bad_inner_class'
+ // CHECK-FIXES: class BadInnerClass;
struct GoodInnerStruct;
struct bad_inner_struct;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for struct 'bad_inner_struct'
+ // CHECK-FIXES: class BadInnerStruct;
union GoodInnerUnion;
union bad_inner_union;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for union 'bad_inner_union'
+ // CHECK-FIXES: class BadInnerUnion;
+
};
// Template forward declarations.
@@ -54,6 +61,7 @@ class GoodTemplateClass;
template <typename T>
class bad_template_class;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'bad_template_class'
+// CHECK-FIXES: class BadTemplateClass;
template <typename T>
struct GoodTemplateStruct;
@@ -61,6 +69,7 @@ struct GoodTemplateStruct;
template <typename T>
struct bad_template_struct;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'bad_template_struct'
+// CHECK-FIXES: class BadTemplateStruct;
template <typename T>
union GoodTemplateUnion;
@@ -68,3 +77,4 @@ union GoodTemplateUnion;
template <typename T>
union bad_template_union;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for union 'bad_template_union'
+// CHECK-FIXES: class BadTemplateUnion;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
index ef02f0f8a200b..f3f8d8e020ab5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
@@ -250,6 +250,8 @@ class my_class;
class my_forward_declared_class;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'my_forward_declared_class'
+// CHECK-FIXES: class MyForwwardDeclaredClass
+
const int my_class::classConstant = 4;
// CHECK-FIXES: const int CMyClass::kClassConstant = 4;
>From 2b252ca2df2033ecf46553434951b5842471e301 Mon Sep 17 00:00:00 2001
From: CarvedCoder <shreyansbehera84 at gmail.com>
Date: Wed, 20 May 2026 16:37:35 +0530
Subject: [PATCH 4/4] Fix class name casing in identifier-naming test
---
.../test/clang-tidy/checkers/readability/identifier-naming.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
index f3f8d8e020ab5..13a133746f47f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp
@@ -250,7 +250,7 @@ class my_class;
class my_forward_declared_class;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'my_forward_declared_class'
-// CHECK-FIXES: class MyForwwardDeclaredClass
+// CHECK-FIXES: class MyForwardDeclaredClass
const int my_class::classConstant = 4;
// CHECK-FIXES: const int CMyClass::kClassConstant = 4;
More information about the cfe-commits
mailing list