Skip to content

Commit f745595

Browse files
committed
[clang-tidy] Improve performance-enum-size to exclude empty enums
Enums without enumerators (empty) are now excluded from analysis as it's not possible to peroperly determinate new narrowed type, and such enums can be used in diffrent way, like as strong-types.
1 parent 573fa77 commit f745595

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace clang::tidy::performance {
2323

2424
namespace {
2525

26+
AST_MATCHER(EnumDecl, hasEnumerators) { return !Node.enumerators().empty(); }
27+
2628
const std::uint64_t Min8 =
2729
std::imaxabs(std::numeric_limits<std::int8_t>::min());
2830
const std::uint64_t Max8 = std::numeric_limits<std::int8_t>::max();
@@ -93,6 +95,7 @@ bool EnumSizeCheck::isLanguageVersionSupported(
9395
void EnumSizeCheck::registerMatchers(MatchFinder *Finder) {
9496
Finder->addMatcher(
9597
enumDecl(unless(isExpansionInSystemHeader()), isDefinition(),
98+
hasEnumerators(),
9699
unless(matchers::matchesAnyListedName(EnumIgnoreList)))
97100
.bind("e"),
98101
this);

clang-tools-extra/docs/clang-tidy/checks/performance/enum-size.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ terms of memory usage and cache performance. However, it's important to
5858
consider the trade-offs and potential impact on code readability and
5959
maintainability.
6060

61+
Enums without enumerators (empty) are excluded from analysis.
62+
6163
Requires C++11 or above.
6264
Does not provide auto-fixes.
6365

clang-tools-extra/test/clang-tidy/checkers/performance/enum-size.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,7 @@ enum class IgnoredSecondEnum
102102
unused2 = 2
103103
};
104104

105+
enum class EnumClassWithoutValues : int {};
106+
enum EnumWithoutValues {};
107+
105108
}

0 commit comments

Comments
 (0)