diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -1140,30 +1140,28 @@ if (Decl->isAnonymousStructOrUnion()) return SK_Invalid; - if (!Decl->getCanonicalDecl()->isThisDeclarationADefinition()) - return SK_Invalid; - - if (Decl->hasDefinition() && Decl->isAbstract() && - NamingStyles[SK_AbstractClass]) - return SK_AbstractClass; + if (const auto *Definition = Decl->getDefinition()) { + if (Definition->isAbstract() && NamingStyles[SK_AbstractClass]) + return SK_AbstractClass; - if (Decl->isStruct() && NamingStyles[SK_Struct]) - return SK_Struct; + if (Definition->isStruct() && NamingStyles[SK_Struct]) + return SK_Struct; - if (Decl->isStruct() && NamingStyles[SK_Class]) - return SK_Class; + if (Definition->isStruct() && NamingStyles[SK_Class]) + return SK_Class; - if (Decl->isClass() && NamingStyles[SK_Class]) - return SK_Class; + if (Definition->isClass() && NamingStyles[SK_Class]) + return SK_Class; - if (Decl->isClass() && NamingStyles[SK_Struct]) - return SK_Struct; + if (Definition->isClass() && NamingStyles[SK_Struct]) + return SK_Struct; - if (Decl->isUnion() && NamingStyles[SK_Union]) - return SK_Union; + if (Definition->isUnion() && NamingStyles[SK_Union]) + return SK_Union; - if (Decl->isEnum() && NamingStyles[SK_Enum]) - return SK_Enum; + if (Definition->isEnum() && NamingStyles[SK_Enum]) + return SK_Enum; + } return SK_Invalid; } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -172,6 +172,10 @@ ` check to provide fixes for ``inline`` namespaces in the same format as :program:`clang-format`. +- Improved the :doc:`readability-identifier-naming + ` check to emit proper + warnings when a type forward declaration precedes its definition. + Removed checks ^^^^^^^^^^^^^^ 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 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp @@ -707,3 +707,19 @@ task ImplicitDeclTest(async_obj &a_object) { co_await a_object; // CHECK-MESSAGES-NOT: warning: invalid case style for local variable } + +// Test scenario when canonical declaration will be a forward declaration +struct ForwardDeclStruct; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for struct 'ForwardDeclStruct' [readability-identifier-naming] +// CHECK-FIXES: {{^}}struct forward_decl_struct; +// CHECK-FIXES: {{^}}struct forward_decl_struct { +struct ForwardDeclStruct { +}; + +struct forward_declared_as_struct; +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: invalid case style for class 'forward_declared_as_struct' [readability-identifier-naming] +// CHECK-FIXES: {{^}}struct CForwardDeclaredAsStruct; +// CHECK-FIXES: {{^}}class CForwardDeclaredAsStruct { +class forward_declared_as_struct { +}; +