Skip to content

[clang-tidy] Fixed bugprone-non-zero-enum-to-bool-conversion #131407

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ namespace clang::tidy::bugprone {

namespace {

AST_MATCHER(EnumDecl, isCompleteAndHasNoZeroValue) {
AST_MATCHER(EnumDecl, isCompleteNonEmptyAndHasNoZeroValue) {
const EnumDecl *Definition = Node.getDefinition();
return Definition && Node.isComplete() &&
Definition->enumerator_begin() != Definition->enumerator_end() &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe Definition->enumerators().empty() is a bit more readable.
it has enumerator_begin != enumerator_end under the hood

std::none_of(Definition->enumerator_begin(),
Definition->enumerator_end(),
[](const EnumConstantDecl *Value) {
return Value->getInitVal().isZero();
});
}

AST_MATCHER(EnumDecl, hasBoolAsUnderlyingType) {
const QualType UnderlyingType = Node.getIntegerType();
return !UnderlyingType.isNull() && UnderlyingType->isBooleanType();
}

} // namespace

NonZeroEnumToBoolConversionCheck::NonZeroEnumToBoolConversionCheck(
Expand Down Expand Up @@ -59,7 +65,8 @@ void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
unless(isExpansionInSystemHeader()), hasType(booleanType()),
hasSourceExpression(
expr(hasType(qualType(hasCanonicalType(hasDeclaration(
enumDecl(isCompleteAndHasNoZeroValue(),
enumDecl(isCompleteNonEmptyAndHasNoZeroValue(),
unless(hasBoolAsUnderlyingType()),
unless(matchers::matchesAnyListedName(
EnumIgnoreList)))
.bind("enum"))))),
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ New check aliases
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^

- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
<clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check to
ignore enums without enumerators and enums with ``bool`` as underlying type.

- Improved :doc:`bugprone-optional-value-conversion
<clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
conversion in argument of ``std::make_optional``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ and maintainability and can result in bugs.
May produce false positives if the ``enum`` is used to store other values
(used as a bit-mask or zero-initialized on purpose). To deal with them,
``// NOLINT`` or casting first to the underlying type before casting to ``bool``
can be used.
can be used. Enums without enumerators and enums that use ``bool`` as the
underlying type are ignored.

It is important to note that this check will not generate warnings if the
definition of the enumeration type is not available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,18 @@ bool testEnumConversion(const EResult& value) {
}

}
}

namespace without::issue {
namespace enum_bool {

enum EResult : bool {
OK = 1
};

bool testEnumConversion(const EResult& value) {
return value;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,10 @@ void testCustomOperator(CustomOperatorEnum e) {
if (!(e & E1)) {}
}

enum EmptyEnum {};

bool testCustomOperator(EmptyEnum value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we should ignore this cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is enum without enumerators = can hold any value = usually used to work as strong type

return value;
}

}