1.. title:: clang-tidy - bugprone-non-zero-enum-to-bool-conversion 2 3bugprone-non-zero-enum-to-bool-conversion 4========================================= 5 6Detect implicit and explicit casts of ``enum`` type into ``bool`` where ``enum`` 7type doesn't have a zero-value enumerator. If the ``enum`` is used only to hold 8values equal to its enumerators, then conversion to ``bool`` will always result 9in ``true`` value. This can lead to unnecessary code that reduces readability 10and maintainability and can result in bugs. 11 12May produce false positives if the ``enum`` is used to store other values 13(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 to ``bool`` 15can be used. 16 17It is important to note that this check will not generate warnings if the 18definition 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 by identifying 22and flagging instances where implicit or explicit casts from enumeration types to 23boolean could cause potential issues. 24 25Example 26------- 27 28.. code-block:: c++ 29 30 enum EStatus { 31 OK = 1, 32 NOT_OK, 33 UNKNOWN 34 }; 35 36 void process(EStatus status) { 37 if (!status) { 38 // this true-branch won't be executed 39 return; 40 } 41 // proceed with "valid data" 42 } 43 44Options 45------- 46 47.. option:: EnumIgnoreList 48 49 Option is used to ignore certain enum types when checking for 50 implicit/explicit casts to bool. It accepts a semicolon-separated list of 51 (fully qualified) enum type names or regular expressions that match the enum 52 type names. 53 The default value is an empty string, which means no enums will be ignored. 54