Detect implicit and explicit conversions of enum to bool,
when enum doesn't have a enumerator with value equal to 0.
In theory such conversion should always return TRUE.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang-tools-extra/clang-tidy/bugprone/EnumToBoolConversionCheck.h | ||
---|---|---|
28 ↗ | (On Diff #497408) | Should language be checked for C++? |
clang-tools-extra/docs/clang-tidy/checks/bugprone/enum-to-bool-conversion.rst | ||
6 ↗ | (On Diff #497408) | Please synchronize first statement with statement in Release Notes. Please use double back-ticks for language constructs. |
49 ↗ | (On Diff #497408) | Please use single back-ticks for option values. Default value is usually placed after option description. |
Ping, Rebase, Changed Option from Regexp to List, Improved documentation, Removed usage of deprecated API
Such a great check, thanks! I have very minor comments, the most debatable one about the name of the check (but no strong opinion).
clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp | ||
---|---|---|
107 | I think the name is a bit generic, do we foresee risk of conflict with other similar checks in the future? I wonder if we should narrow it down to something like bugprone-non-zero-enum-to-bool-conversion. WDYT? | |
clang-tools-extra/clang-tidy/bugprone/EnumToBoolConversionCheck.cpp | ||
23 ↗ | (On Diff #502752) | Use explicit type |
24 ↗ | (On Diff #502752) | The convention in the LLVM repo is to use traditional operators, please keep consistency. |
27 ↗ | (On Diff #502752) | Use explicit type |
72 ↗ | (On Diff #502752) | Unnecessary empty line |
clang-tools-extra/docs/ReleaseNotes.rst | ||
123 | Keep alphabetical order. | |
clang-tools-extra/docs/clang-tidy/checks/bugprone/enum-to-bool-conversion.rst | ||
12–13 ↗ | (On Diff #502752) | I can't think of a case where this could happen, might be worth adding an example below. |
14 ↗ | (On Diff #502752) | Nit: Add space between // and NOLINT |
38 ↗ | (On Diff #502752) | Nit: use 2 chars indentation |
39 ↗ | (On Diff #502752) | Use traditional operators for consistency. |
41 ↗ | (On Diff #502752) | won't |
41 ↗ | (On Diff #502752) | Remove? |
clang-tools-extra/test/clang-tidy/checkers/bugprone/enum-to-bool-conversion-cpp11.cpp | ||
9 ↗ | (On Diff #502752) | Indent with 2 chars instead of 4. |
16 ↗ | (On Diff #502752) | Nit: Add 2 chars indentation to align with the code. |
20 ↗ | (On Diff #502752) | Maybe test also "int", since it's the most common default? Also test one enum class without explicit underlying type. |
Thank you for review, I will correct comments in this week.
clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp | ||
---|---|---|
107 | I'm fine with name change.... | |
clang-tools-extra/docs/clang-tidy/checks/bugprone/enum-to-bool-conversion.rst | ||
41 ↗ | (On Diff #502752) | enums are ints, you can assign them any value by using static casts or when reading data from external message, so thats why in theory, but in this example you right, there is no "in theory" |
clang-tools-extra/docs/clang-tidy/checks/bugprone/enum-to-bool-conversion.rst | ||
---|---|---|
12–13 ↗ | (On Diff #502752) | I thinkg that in STL we got some enums (flags) used as bitmasks... Just wanted to mention that there could be some known false-positives. |
LGTM, thanks! Would be good to fix the last nit before landing.
clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.h | ||
---|---|---|
18 | Nit remove excessive whitespace |
I think the name is a bit generic, do we foresee risk of conflict with other similar checks in the future? I wonder if we should narrow it down to something like bugprone-non-zero-enum-to-bool-conversion. WDYT?