[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-02 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp created 
https://github.com/llvm/llvm-project/pull/138282

Warn on non-class enum definitions as suggested by the Core Guidelines: 
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class

>From 9e2d442e38d2e4ec28c4bbd432bab25aaa6fee51 Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index 4f68c487cac9d..92e6866c5f17b 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseNodiscardCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index 1860759332063..30e6938dbd7db 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseNodiscardCheck.h"
@@ -107,6 +108,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 
CheckFactories.registerCheck("modernize-use-equals-default");
 CheckFactories.registerCheck(
 "modernize-use-equals-delete");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llv

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-12 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 0567bc8e1168bb409ee759dd5505861a644a8ead Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 1/7] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index fc46c72982fdc..1f77c9a94d25a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 
CheckFactories.registerCheck("modernize-use-equals-default");
 CheckFactories.registerCheck(
 "modernize-use-equals-delete");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//==

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-12 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 0567bc8e1168bb409ee759dd5505861a644a8ead Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 1/8] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index fc46c72982fdc..1f77c9a94d25a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 
CheckFactories.registerCheck("modernize-use-equals-default");
 CheckFactories.registerCheck(
 "modernize-use-equals-delete");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//==

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-12 Thread Philipp Jung via cfe-commits


@@ -117,6 +117,11 @@ Improvements to clang-tidy
 New checks
 ^^
 
+- New :doc:`cppcoreguidelines-use-enum-class

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-12 Thread Philipp Jung via cfe-commits


@@ -198,7 +203,7 @@ Changes in existing checks
   diagnosing designated initializers for ``std::array`` initializations.
 
 - Improved :doc:`modernize-use-ranges
-  ` check by updating suppress 
+  ` check by updating suppress

JungPhilipp wrote:

Done. Was added by mistake (I configured my IDE to remove trailing whitespace 
on save)

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,13 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s 
cppcoreguidelines-use-enum-class %t -- -config="{CheckOptions: 
{cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: true}}" --
+
+enum E {};
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum 
class' instead [cppcoreguidelines-use-enum-class]
+
+enum class EC {};
+
+struct S {
+  enum E {};

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,13 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s 
cppcoreguidelines-use-enum-class %t -- -config="{CheckOptions: 
{cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: true}}" --

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,31 @@
+.. title:: clang-tidy - cppcoreguidelines-use-enum-class
+
+cppcoreguidelines-use-enum-class
+=

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,31 @@
+.. title:: clang-tidy - cppcoreguidelines-use-enum-class
+
+cppcoreguidelines-use-enum-class
+=
+
+Finds plain non-class ``enum`` definitions that could use ``enum class``.
+
+This check implements `Enum.3
+`_
+from the C++ Core Guidelines."
+
+Example:
+
+.. code-block:: c++
+
+  enum E {};// use "enum class E {};" instead
+  enum class E {};  // OK
+
+  struct S {
+  enum E {};// use "enum class E {};" instead
+// OK with option IgnoreUnscopedEnumsInClasses
+  };
+
+  namespace N {
+  enum E {};// use "enum class E {};" instead
+  }
+
+

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -59,6 +60,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
 CheckFactories.registerCheck(
 "cppcoreguidelines-avoid-capturing-lambda-coroutines");
+CheckFactories.registerCheck(
+"cppcoreguidelines-use-enum-class");

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -136,6 +136,11 @@ New checks
   Finds potentially erroneous calls to ``reset`` method on smart pointers when
   the pointee type also has a ``reset`` method.
 
+- New :doc:`cppcoreguidelines-use-enum-class

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -211,6 +211,7 @@ Clang-Tidy Checks
:doc:`cppcoreguidelines-rvalue-reference-param-not-moved 
`,
:doc:`cppcoreguidelines-slicing `,
:doc:`cppcoreguidelines-special-member-functions 
`,
+   :doc:`cppcoreguidelines-use-enum-class `, 
"Yes"

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,31 @@
+.. title:: clang-tidy - cppcoreguidelines-use-enum-class
+
+cppcoreguidelines-use-enum-class
+=
+
+Finds plain non-class ``enum`` definitions that could use ``enum class``.
+
+This check implements `Enum.3
+`_
+from the C++ Core Guidelines."
+
+Example:
+
+.. code-block:: c++
+
+  enum E {};// use "enum class E {};" instead
+  enum class E {};  // OK
+
+  struct S {
+  enum E {};// use "enum class E {};" instead
+// OK with option IgnoreUnscopedEnumsInClasses
+  };
+
+  namespace N {
+  enum E {};// use "enum class E {};" instead
+  }
+
+
+.. option:: IgnoreUnscopedEnumsInClasses
+
+   When `true` (default is `false`), ignores unscoped ``enum`` declarations in 
classes.

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,13 @@
+// RUN: %check_clang_tidy -std=c++11-or-later %s 
cppcoreguidelines-use-enum-class %t -- -config="{CheckOptions: 
{cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: true}}" --
+
+enum E {};
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum 
class' instead [cppcoreguidelines-use-enum-class]

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,31 @@
+.. title:: clang-tidy - cppcoreguidelines-use-enum-class
+
+cppcoreguidelines-use-enum-class
+=
+
+Finds plain non-class ``enum`` definitions that could use ``enum class``.

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 0567bc8e1168bb409ee759dd5505861a644a8ead Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 1/6] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index fc46c72982fdc..1f77c9a94d25a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 
CheckFactories.registerCheck("modernize-use-equals-default");
 CheckFactories.registerCheck(
 "modernize-use-equals-delete");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//==

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp edited 
https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 0567bc8e1168bb409ee759dd5505861a644a8ead Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 1/5] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index fc46c72982fdc..1f77c9a94d25a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 
CheckFactories.registerCheck("modernize-use-equals-default");
 CheckFactories.registerCheck(
 "modernize-use-equals-delete");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//==

[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,26 @@
+.. title:: clang-tidy - modernize-use-enum-class
+
+modernize-use-enum-class
+=
+
+Scoped enums (enum class) should be preferred over unscoped enums:

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");

JungPhilipp wrote:

Dropped for now. Done.

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,58 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s modernize-use-enum-class %t

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,

JungPhilipp wrote:

Done.

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -115,6 +115,11 @@ New checks
   Gives warnings for tagged unions, where the number of tags is
   different from the number of data members inside the union.
 
+- New :doc:`modernize-use-enum-class
+  ` check.
+
+  Finds plain non-class enum definitions that could use ``enum class``.

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USEENUMCLASSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USEENUMCLASSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::modernize {
+
+/// Check for unscoped enums that are not contained in classes/structs.
+/// Suggest to use scoped enums (enum class) instead.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-enum-class.html
+class UseEnumClassCheck : public ClangTidyCheck {
+public:
+  UseEnumClassCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus;

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,26 @@
+.. title:: clang-tidy - modernize-use-enum-class
+
+modernize-use-enum-class
+=
+
+Scoped enums (enum class) should be preferred over unscoped enums:
+https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()

JungPhilipp wrote:

Added an option. Done.

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits

JungPhilipp wrote:

> Your branch seems to be way behind current `main`, and 
> `clang-tools-extra/docs/ReleaseNotes.rst` seems to be from already released 
> 20th version of LLVM. (I don't know how can it even show some kind of diff) 
> Could you please rebase on fresh `main` branch.

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'modernize-use-enum-class' (PR #138282)

2025-05-09 Thread Philipp Jung via cfe-commits


@@ -0,0 +1,26 @@
+.. title:: clang-tidy - modernize-use-enum-class
+
+modernize-use-enum-class
+=
+
+Scoped enums (enum class) should be preferred over unscoped enums:
+https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
+
+Unscoped enums in classes are not reported since it is a well
+established pattern to limit the scope of plain enums.

JungPhilipp wrote:

Done

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-19 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 0567bc8e1168bb409ee759dd5505861a644a8ead Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 1/9] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index fc46c72982fdc..1f77c9a94d25a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 
CheckFactories.registerCheck("modernize-use-equals-default");
 CheckFactories.registerCheck(
 "modernize-use-equals-delete");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//==

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-19 Thread Philipp Jung via cfe-commits


@@ -130,6 +130,11 @@ New checks
   Finds unintended character output from ``unsigned char`` and ``signed char``
   to an ``ostream``.
 
+- New :doc:`cppcoreguidelines-use-enum-class
+  ` check.
+
+  Finds plain non-class ``enum`` definitions that could use ``enum class``.

JungPhilipp wrote:

Missed that. Thank you.

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-05-16 Thread Philipp Jung via cfe-commits

JungPhilipp wrote:

Thanks to everybody for the quick and thorough reviews. I really appreciate the 
help!
Is there anything else to do for me at this point?

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-06-13 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 414fd293764c04bc86154f2d7b42c31a3a0ea693 Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 01/11] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index 0cf59b6e0216a..05308f2436f7a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 CheckFactories.registerCheck(
 "modernize-use-equals-default");
 CheckFactories.registerCheck(
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-06-13 Thread Philipp Jung via cfe-commits

JungPhilipp wrote:

> Also, please rebase (there are some merge conflicts), and also mark 
> conversations as resolved (I see there's a few remaining)

Done, ready-for-review

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-06-13 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 0567bc8e1168bb409ee759dd5505861a644a8ead Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 01/10] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index fc46c72982fdc..1f77c9a94d25a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 
CheckFactories.registerCheck("modernize-use-equals-default");
 CheckFactories.registerCheck(
 "modernize-use-equals-delete");
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-06-13 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 414fd293764c04bc86154f2d7b42c31a3a0ea693 Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 01/10] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index 0cf59b6e0216a..05308f2436f7a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 CheckFactories.registerCheck(
 "modernize-use-equals-default");
 CheckFactories.registerCheck(
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-06-13 Thread Philipp Jung via cfe-commits

https://github.com/JungPhilipp updated 
https://github.com/llvm/llvm-project/pull/138282

>From 414fd293764c04bc86154f2d7b42c31a3a0ea693 Mon Sep 17 00:00:00 2001
From: Philipp Jung 
Date: Fri, 2 May 2025 15:22:40 +0200
Subject: [PATCH 01/12] Add check 'modernize-use-enum-class'

Warn on non-class enum definitions as suggested by the Core Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class
---
 .../clang-tidy/modernize/CMakeLists.txt   |  1 +
 .../modernize/ModernizeTidyModule.cpp |  2 +
 .../modernize/UseEnumClassCheck.cpp   | 34 +++
 .../clang-tidy/modernize/UseEnumClassCheck.h  | 34 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/modernize/use-enum-class.rst   | 26 +
 .../checkers/modernize/use-enum-class.cpp | 58 +++
 8 files changed, 161 insertions(+)
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/modernize/use-enum-class.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/modernize/use-enum-class.cpp

diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
index bab1167fb15ff..ea19586b1f08c 100644
--- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt
@@ -34,6 +34,7 @@ add_clang_library(clangTidyModernizeModule STATIC
   UseDefaultMemberInitCheck.cpp
   UseDesignatedInitializersCheck.cpp
   UseEmplaceCheck.cpp
+  UseEnumClassCheck.cpp
   UseEqualsDefaultCheck.cpp
   UseEqualsDeleteCheck.cpp
   UseIntegerSignComparisonCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp 
b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index 0cf59b6e0216a..05308f2436f7a 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -35,6 +35,7 @@
 #include "UseDefaultMemberInitCheck.h"
 #include "UseDesignatedInitializersCheck.h"
 #include "UseEmplaceCheck.h"
+#include "UseEnumClassCheck.h"
 #include "UseEqualsDefaultCheck.h"
 #include "UseEqualsDeleteCheck.h"
 #include "UseIntegerSignComparisonCheck.h"
@@ -110,6 +111,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");
 CheckFactories.registerCheck(
 "modernize-use-equals-default");
 CheckFactories.registerCheck(
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
new file mode 100644
index 0..9fc3614aaf498
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.cpp
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseEnumClassCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  traverse(TK_AsIs,
+   enumDecl(unless(isScoped()), unless(hasParent(recordDecl()
+  .bind("unscoped_enum"),
+  this);
+}
+
+void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *UnscopedEnum = Result.Nodes.getNodeAs("unscoped_enum");
+
+  diag(UnscopedEnum->getLocation(),
+   "enum %0 is unscoped, use enum class instead")
+  << UnscopedEnum;
+  diag(UnscopedEnum->getLocation(), "insert 'class'", DiagnosticIDs::Note)
+  << FixItHint::CreateInsertion(UnscopedEnum->getLocation(), "class ");
+}
+
+} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
new file mode 100644
index 0..9cfb2024b9cfd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/UseEnumClassCheck.h
@@ -0,0 +1,34 @@
+//===--- UseEnumClassCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-

[clang-tools-extra] Add check 'cppcoreguidelines-use-enum-class' (PR #138282)

2025-06-13 Thread Philipp Jung via cfe-commits


@@ -110,6 +110,7 @@ class ModernizeModule : public ClangTidyModule {
 CheckFactories.registerCheck(
 "modernize-use-default-member-init");
 CheckFactories.registerCheck("modernize-use-emplace");
+
CheckFactories.registerCheck("modernize-use-enum-class");

JungPhilipp wrote:

Wrong conflict resolution, remove

https://github.com/llvm/llvm-project/pull/138282
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits