54 lines · plain
1.. title:: clang-tidy - bugprone-non-zero-enum-to-bool-conversion2 3bugprone-non-zero-enum-to-bool-conversion4=========================================5 6Detect implicit and explicit casts of ``enum`` type into ``bool`` where7``enum`` type doesn't have a zero-value enumerator. If the ``enum`` is used8only to hold values equal to its enumerators, then conversion to ``bool`` will9always result in ``true`` value. This can lead to unnecessary code that reduces10readability and maintainability and can result in bugs.11 12May produce false positives if the ``enum`` is used to store other values13(used as a bit-mask or zero-initialized on purpose). To deal with them,14``// NOLINT`` or casting first to the underlying type before casting to15``bool`` can be used.16 17It is important to note that this check will not generate warnings if the18definition of the enumeration type is not available.19Additionally, C++11 enumeration classes are supported by this check.20 21Overall, this check serves to improve code quality and readability by22identifying and flagging instances where implicit or explicit casts from23enumeration types to boolean could cause potential issues.24 25Example26-------27 28.. code-block:: c++29 30 enum EStatus {31 OK = 1,32 NOT_OK,33 UNKNOWN34 };35 36 void process(EStatus status) {37 if (!status) {38 // this true-branch won't be executed39 return;40 }41 // proceed with "valid data"42 }43 44Options45-------46 47.. option:: EnumIgnoreList48 49 Option is used to ignore certain enum types when checking for50 implicit/explicit casts to bool. It accepts a semicolon-separated list of51 (fully qualified) enum type names or regular expressions that match the enum52 type names.53 The default value is an empty string, which means no enums will be ignored.54