[clang-tools-extra] 29316be - [clang-tidy] Fix `cppcoreguidelines-use-enum-class` anonymous enum bug (#215352)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 12 11:13:42 PDT 2026
Author: void_17
Date: 2026-08-12T21:13:36+03:00
New Revision: 29316bece363e3bd09140968d62d29958cd4990d
URL: https://github.com/llvm/llvm-project/commit/29316bece363e3bd09140968d62d29958cd4990d
DIFF: https://github.com/llvm/llvm-project/commit/29316bece363e3bd09140968d62d29958cd4990d.diff
LOG: [clang-tidy] Fix `cppcoreguidelines-use-enum-class` anonymous enum bug (#215352)
`UseEnumClassCheck::registerMatchers` implementing
`cppcoreguidelines-use-enum-class` didn't check for empty enum names and
suggested erroneous "fix" to make anonymous enums `class enum`, which is
ill-defined, fix this.
Fixes #215328
Added:
Modified:
clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.cpp
clang-tools-extra/docs/ReleaseNotes.md
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-enum-class.rst
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/use-enum-class.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.cpp
index 84720d10c233e..0865148a2fad4 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.cpp
@@ -10,8 +10,15 @@
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
+using namespace clang::ast_matchers::internal;
namespace clang::tidy::cppcoreguidelines {
+namespace {
+// FIXME: The matcher 'hasName(Name)' asserts that its argument 'Name' is
+// nonempty. Perhaps remove that assertion and replace 'isUnnamed()' with
+// 'hasName("")'.
+AST_MATCHER(EnumDecl, isUnnamed) { return Node.getName().empty(); }
+} // namespace
UseEnumClassCheck::UseEnumClassCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
@@ -26,10 +33,10 @@ void UseEnumClassCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
}
void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
- const auto EnumDecl =
- IgnoreUnscopedEnumsInClasses
- ? enumDecl(unless(isScoped()), unless(hasParent(recordDecl())))
- : enumDecl(unless(isScoped()));
+ const auto EnumDecl = IgnoreUnscopedEnumsInClasses
+ ? enumDecl(unless(isScoped()), unless(isUnnamed()),
+ unless(hasParent(recordDecl())))
+ : enumDecl(unless(isScoped()), unless(isUnnamed()));
Finder->addMatcher(EnumDecl.bind("unscoped_enum"), this);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index f72beb0810b19..ef3e6c49ce172 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -121,6 +121,9 @@ infrastructure are described first, followed by tool-specific sections.
- Improved {doc}`cppcoreguidelines-pro-type-member-init
<clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check by treating
`std::array` the same as built-in arrays when `IgnoreArrays` option is enabled.
+
+- Improved {doc}`cppcoreguidelines-use-enum-class
+ <clang-tidy/checks/cppcoreguidelines/use-enum-class>` check by omitting unnamed enums from the `enum class` requirement, as previously the check suggested users an ill-formed fix.
- Improved {doc}`misc-const-correctness
<clang-tidy/checks/misc/const-correctness>` check by fixing false positives
diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-enum-class.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-enum-class.rst
index 746d24aa8ea34..dd0f3fc25fa8b 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-enum-class.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-enum-class.rst
@@ -4,7 +4,8 @@ cppcoreguidelines-use-enum-class
================================
Finds unscoped (non-class) ``enum`` declarations and suggests using
-``enum class`` instead.
+``enum class`` instead. Unnamed enum are ignored and will be handled by check implementing `Enum.6
+<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#enum6-avoid-unnamed-enumerations>`_.
This check implements `Enum.3
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#renum-class>`_
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/use-enum-class.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/use-enum-class.cpp
index f53d787f80efa..033865a7ac830 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/use-enum-class.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/use-enum-class.cpp
@@ -60,3 +60,5 @@ enum ForwardE : int;
enum class ForwardEC : int;
enum struct ForwardES : int;
+
+enum { A };
More information about the cfe-commits
mailing list