-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Add check 'cppcoreguidelines-use-enum-class' #138282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
carlosgalvezp
merged 12 commits into
llvm:main
from
JungPhilipp:clang-tidy_modernize_enum_class
Jun 18, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
414fd29
Add check 'modernize-use-enum-class'
JungPhilipp baeed94
Move check to cppcoreguidelines- section
JungPhilipp 61665e0
Remove traverse call
JungPhilipp b9454d4
Add option to ignore unscoped enums in classes
JungPhilipp 96cb2fb
Improve documentation
JungPhilipp b024d6e
Address review comments
JungPhilipp 19a799a
Fix order of checks in docu
JungPhilipp 7bb1043
Revert remove whitespace
JungPhilipp 7432684
Make release notes match docu
JungPhilipp 1c2ee44
Move checks for use-enum-class to single file
JungPhilipp 4f2ea3f
Fixup formatting after rebase
JungPhilipp fa7d6c0
Fix conflict resolution during rebase
JungPhilipp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===--- 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::cppcoreguidelines { | ||
|
||
UseEnumClassCheck::UseEnumClassCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context), | ||
IgnoreUnscopedEnumsInClasses( | ||
Options.get("IgnoreUnscopedEnumsInClasses", false)) {} | ||
|
||
void UseEnumClassCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { | ||
Options.store(Opts, "IgnoreUnscopedEnumsInClasses", | ||
IgnoreUnscopedEnumsInClasses); | ||
} | ||
|
||
void UseEnumClassCheck::registerMatchers(MatchFinder *Finder) { | ||
auto EnumDecl = | ||
IgnoreUnscopedEnumsInClasses | ||
? enumDecl(unless(isScoped()), unless(hasParent(recordDecl()))) | ||
: enumDecl(unless(isScoped())); | ||
Finder->addMatcher(EnumDecl.bind("unscoped_enum"), this); | ||
} | ||
|
||
void UseEnumClassCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *UnscopedEnum = Result.Nodes.getNodeAs<EnumDecl>("unscoped_enum"); | ||
|
||
diag(UnscopedEnum->getLocation(), | ||
"enum %0 is unscoped, use 'enum class' instead") | ||
<< UnscopedEnum; | ||
} | ||
|
||
} // namespace clang::tidy::cppcoreguidelines |
40 changes: 40 additions & 0 deletions
40
clang-tools-extra/clang-tidy/cppcoreguidelines/UseEnumClassCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===--- 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_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::cppcoreguidelines { | ||
|
||
/// Finds unscoped (non-class) enum declarations and suggests using enum class | ||
/// instead. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/use-enum-class.html | ||
class UseEnumClassCheck : public ClangTidyCheck { | ||
public: | ||
UseEnumClassCheck(StringRef Name, ClangTidyContext *Context); | ||
void storeOptions(ClangTidyOptions::OptionMap &Opts) override; | ||
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.CPlusPlus11; | ||
} | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TraversalKind::TK_IgnoreUnlessSpelledInSource; | ||
} | ||
|
||
private: | ||
const bool IgnoreUnscopedEnumsInClasses; | ||
}; | ||
|
||
} // namespace clang::tidy::cppcoreguidelines | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_USEENUMCLASSCHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-enum-class.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.. title:: clang-tidy - cppcoreguidelines-use-enum-class | ||
|
||
cppcoreguidelines-use-enum-class | ||
================================ | ||
|
||
Finds unscoped (non-class) ``enum`` declarations and suggests using | ||
``enum class`` instead. | ||
|
||
This check implements `Enum.3 | ||
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-class>`_ | ||
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 | ||
} | ||
|
||
Options | ||
------- | ||
|
||
JungPhilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.. option:: IgnoreUnscopedEnumsInClasses | ||
|
||
When `true`, ignores unscoped ``enum`` declarations in classes. | ||
Default is `false`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/use-enum-class.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=ALL,DEFAULT %s \ | ||
// RUN: cppcoreguidelines-use-enum-class %t -- | ||
|
||
// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=ALL %s \ | ||
// RUN: cppcoreguidelines-use-enum-class %t -- \ | ||
// RUN: -config="{CheckOptions: { \ | ||
// RUN: cppcoreguidelines-use-enum-class.IgnoreUnscopedEnumsInClasses: true \ | ||
// RUN: }}" -- | ||
|
||
enum E {}; | ||
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead | ||
|
||
enum class EC {}; | ||
|
||
enum struct ES {}; | ||
|
||
struct S { | ||
enum E {}; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
class C { | ||
enum E {}; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
template<class T> | ||
class TC { | ||
enum E {}; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
union U { | ||
enum E {}; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
}; | ||
|
||
namespace { | ||
enum E {}; | ||
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
} // namespace | ||
|
||
namespace N { | ||
enum E {}; | ||
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'E' is unscoped, use 'enum class' instead | ||
enum class EC {}; | ||
} // namespace N | ||
|
||
template<enum ::EC> | ||
static void foo(); | ||
|
||
enum ForwardE : int; | ||
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: enum 'ForwardE' is unscoped, use 'enum class' instead | ||
|
||
enum class ForwardEC : int; | ||
JungPhilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
enum struct ForwardES : int; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.