Skip to content

Commit f54b035

Browse files
committed
[clang-tidy] Fixed bugprone-non-zero-enum-to-bool-conversion
Improved bugprone-non-zero-enum-to-bool-conversion check to ignore enums without enumerators and enums with bool as underlying type. Fixes #130762
1 parent 471f034 commit f54b035

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@ namespace clang::tidy::bugprone {
1919

2020
namespace {
2121

22-
AST_MATCHER(EnumDecl, isCompleteAndHasNoZeroValue) {
22+
AST_MATCHER(EnumDecl, isCompleteNonEmptyAndHasNoZeroValue) {
2323
const EnumDecl *Definition = Node.getDefinition();
2424
return Definition && Node.isComplete() &&
25+
Definition->enumerator_begin() != Definition->enumerator_end() &&
2526
std::none_of(Definition->enumerator_begin(),
2627
Definition->enumerator_end(),
2728
[](const EnumConstantDecl *Value) {
2829
return Value->getInitVal().isZero();
2930
});
3031
}
3132

33+
AST_MATCHER(EnumDecl, hasBoolAsUnderlyingType) {
34+
const QualType UnderlyingType = Node.getIntegerType();
35+
return !UnderlyingType.isNull() && UnderlyingType->isBooleanType();
36+
}
37+
3238
} // namespace
3339

3440
NonZeroEnumToBoolConversionCheck::NonZeroEnumToBoolConversionCheck(
@@ -59,7 +65,8 @@ void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
5965
unless(isExpansionInSystemHeader()), hasType(booleanType()),
6066
hasSourceExpression(
6167
expr(hasType(qualType(hasCanonicalType(hasDeclaration(
62-
enumDecl(isCompleteAndHasNoZeroValue(),
68+
enumDecl(isCompleteNonEmptyAndHasNoZeroValue(),
69+
unless(hasBoolAsUnderlyingType()),
6370
unless(matchers::matchesAnyListedName(
6471
EnumIgnoreList)))
6572
.bind("enum"))))),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ New check aliases
118118
Changes in existing checks
119119
^^^^^^^^^^^^^^^^^^^^^^^^^^
120120

121+
- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
122+
<clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check to
123+
ignore enums without enumerators and enums with ``bool`` as underlying type.
124+
121125
- Improved :doc:`bugprone-optional-value-conversion
122126
<clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
123127
conversion in argument of ``std::make_optional``.

clang-tools-extra/docs/clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ and maintainability and can result in bugs.
1212
May produce false positives if the ``enum`` is used to store other values
1313
(used as a bit-mask or zero-initialized on purpose). To deal with them,
1414
``// NOLINT`` or casting first to the underlying type before casting to ``bool``
15-
can be used.
15+
can be used. Enums without enumerators and enums that use ``bool`` as the
16+
underlying type are ignored.
1617

1718
It is important to note that this check will not generate warnings if the
1819
definition of the enumeration type is not available.

clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion-cpp11.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,18 @@ bool testEnumConversion(const EResult& value) {
106106
}
107107

108108
}
109+
}
110+
111+
namespace without::issue {
112+
namespace enum_bool {
109113

114+
enum EResult : bool {
115+
OK = 1
116+
};
117+
118+
bool testEnumConversion(const EResult& value) {
119+
return value;
120+
}
121+
122+
}
110123
}

clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,10 @@ void testCustomOperator(CustomOperatorEnum e) {
126126
if (!(e & E1)) {}
127127
}
128128

129+
enum EmptyEnum {};
130+
131+
bool testCustomOperator(EmptyEnum value) {
132+
return value;
133+
}
134+
129135
}

0 commit comments

Comments
 (0)