Skip to content

Commit 503e315

Browse files
authored
[clang-tidy] Exclude bitwise operators in bugprone-non-zero-enum-to-bool-conversion (#65498)
Improved bugprone-non-zero-enum-to-bool-conversion check by eliminating false positives resulting from direct usage of bitwise operators.
1 parent 76c09d9 commit 503e315

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ bool NonZeroEnumToBoolConversionCheck::isLanguageVersionSupported(
4949
}
5050

5151
void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
52+
// Excluding bitwise operators (binary and overload) to avoid false-positives
53+
// in code like this 'if (e & SUCCESS) {'.
54+
auto ExcludedOperators = binaryOperation(hasAnyOperatorName(
55+
"|", "&", "^", "<<", ">>", "~", "|=", "&=", "^=", "<<=", ">>="));
56+
5257
Finder->addMatcher(
5358
castExpr(hasCastKind(CK_IntegralToBoolean),
5459
unless(isExpansionInSystemHeader()), hasType(booleanType()),
@@ -58,7 +63,8 @@ void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
5863
unless(matchers::matchesAnyListedName(
5964
EnumIgnoreList)))
6065
.bind("enum"))))),
61-
unless(declRefExpr(to(enumConstantDecl()))))),
66+
unless(declRefExpr(to(enumConstantDecl()))),
67+
unless(ignoringImplicit(ExcludedOperators)))),
6268
unless(hasAncestor(staticAssertDecl())))
6369
.bind("cast"),
6470
this);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ Changes in existing checks
183183
<clang-tidy/checks/bugprone/lambda-function-name>` check by adding option
184184
`IgnoreMacros` to ignore warnings in macros.
185185

186+
- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
187+
<clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check by
188+
eliminating false positives resulting from direct usage of bitwise operators.
189+
186190
- Improved :doc:`bugprone-reserved-identifier
187191
<clang-tidy/checks/bugprone/reserved-identifier>` check, so that it does not
188192
warn on macros starting with underscore and lowercase letter.

clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ bool explicitCompare(EStatus value) {
8282
return value == SUCCESS;
8383
}
8484

85+
bool explicitBitUsage1(EStatus value) {
86+
return (value & SUCCESS);
87+
}
88+
89+
bool explicitBitUsage2(EStatus value) {
90+
return (value | SUCCESS);
91+
}
92+
8593
bool testEnumeratorCompare() {
8694
return SUCCESS;
8795
}
@@ -104,4 +112,16 @@ bool testIgnored(IgnoredSecondEnum value) {
104112
return value;
105113
}
106114

115+
enum CustomOperatorEnum {
116+
E0 = 0x1,
117+
E1 = 0x2,
118+
E2 = 0x4
119+
};
120+
121+
CustomOperatorEnum operator&(CustomOperatorEnum a, CustomOperatorEnum b) { return static_cast<CustomOperatorEnum>(a & b); }
122+
123+
void testCustomOperator(CustomOperatorEnum e) {
124+
if (e & E1) {}
125+
}
126+
107127
}

0 commit comments

Comments
 (0)