| 1 |
.. title:: clang-tidy - bugprone-non-zero-enum-to-bool-conversion |
| 2 |
|
| 3 |
bugprone-non-zero-enum-to-bool-conversion |
| 4 |
========================================= |
| 5 |
|
| 6 |
Detect implicit and explicit casts of ``enum`` type into ``bool`` where ``enum`` |
| 7 |
type doesn't have a zero-value enumerator. If the ``enum`` is used only to hold |
| 8 |
values equal to its enumerators, then conversion to ``bool`` will always result |
| 9 |
in ``true`` value. This can lead to unnecessary code that reduces readability |
| 10 |
and maintainability and can result in bugs. |
| 11 |
|
| 12 |
May 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`` |
| 15 |
can be used. |
| 16 |
|
| 17 |
It is important to note that this check will not generate warnings if the |
| 18 |
definition of the enumeration type is not available. |
| 19 |
Additionally, C++11 enumeration classes are supported by this check. |
| 20 |
|
| 21 |
Overall, this check serves to improve code quality and readability by identifying |
| 22 |
and flagging instances where implicit or explicit casts from enumeration types to |
| 23 |
boolean could cause potential issues. |
| 24 |
|
| 25 |
Example |
| 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 |
|
| 44 |
Options |
| 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. |