Skip to content

Commit

Permalink
[clang-tidy] Improve performance-enum-size to exclude empty enums (#7…
Browse files Browse the repository at this point in the history
…1640)

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.

Closes #71544
  • Loading branch information
PiotrZSL committed Nov 8, 2023
1 parent a141a9f commit 3716b5b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/performance/EnumSizeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace clang::tidy::performance {

namespace {

AST_MATCHER(EnumDecl, hasEnumerators) { return !Node.enumerators().empty(); }

const std::uint64_t Min8 =
std::imaxabs(std::numeric_limits<std::int8_t>::min());
const std::uint64_t Max8 = std::numeric_limits<std::int8_t>::max();
Expand Down Expand Up @@ -93,6 +95,7 @@ bool EnumSizeCheck::isLanguageVersionSupported(
void EnumSizeCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
enumDecl(unless(isExpansionInSystemHeader()), isDefinition(),
hasEnumerators(),
unless(matchers::matchesAnyListedName(EnumIgnoreList)))
.bind("e"),
this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ terms of memory usage and cache performance. However, it's important to
consider the trade-offs and potential impact on code readability and
maintainability.

Enums without enumerators (empty) are excluded from analysis.

Requires C++11 or above.
Does not provide auto-fixes.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ enum class IgnoredSecondEnum
unused2 = 2
};

enum class EnumClassWithoutValues : int {};
enum EnumWithoutValues {};

}

0 comments on commit 3716b5b

Please sign in to comment.