[clang-tools-extra] 503e315 - [clang-tidy] Exclude bitwise operators in bugprone-non-zero-enum-to-bool-conversion (#65498)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 10 09:08:55 PDT 2023


Author: Piotr Zegar
Date: 2023-09-10T18:08:51+02:00
New Revision: 503e31528a81d407ae76071bbc98fdc627a0bd29

URL: https://github.com/llvm/llvm-project/commit/503e31528a81d407ae76071bbc98fdc627a0bd29
DIFF: https://github.com/llvm/llvm-project/commit/503e31528a81d407ae76071bbc98fdc627a0bd29.diff

LOG: [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.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
index a59c4fc47c0b4e9..918b6e3824f0b1b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
@@ -49,6 +49,11 @@ bool NonZeroEnumToBoolConversionCheck::isLanguageVersionSupported(
 }
 
 void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
+  // Excluding bitwise operators (binary and overload) to avoid false-positives
+  // in code like this 'if (e & SUCCESS) {'.
+  auto ExcludedOperators = binaryOperation(hasAnyOperatorName(
+      "|", "&", "^", "<<", ">>", "~", "|=", "&=", "^=", "<<=", ">>="));
+
   Finder->addMatcher(
       castExpr(hasCastKind(CK_IntegralToBoolean),
                unless(isExpansionInSystemHeader()), hasType(booleanType()),
@@ -58,7 +63,8 @@ void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
                                      unless(matchers::matchesAnyListedName(
                                          EnumIgnoreList)))
                                 .bind("enum"))))),
-                        unless(declRefExpr(to(enumConstantDecl()))))),
+                        unless(declRefExpr(to(enumConstantDecl()))),
+                        unless(ignoringImplicit(ExcludedOperators)))),
                unless(hasAncestor(staticAssertDecl())))
           .bind("cast"),
       this);

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index c1a36ac0f92a96a..63c644ca1a52239 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/lambda-function-name>` check by adding option
   `IgnoreMacros` to ignore warnings in macros.
 
+- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
+  <clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check by
+  eliminating false positives resulting from direct usage of bitwise operators.
+
 - Improved :doc:`bugprone-reserved-identifier
   <clang-tidy/checks/bugprone/reserved-identifier>` check, so that it does not
   warn on macros starting with underscore and lowercase letter.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion.cpp
index e0728680d4c6ad1..794e7b20c8ca53c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/non-zero-enum-to-bool-conversion.cpp
@@ -82,6 +82,14 @@ bool explicitCompare(EStatus value) {
   return value == SUCCESS;
 }
 
+bool explicitBitUsage1(EStatus value) {
+  return (value & SUCCESS);
+}
+
+bool explicitBitUsage2(EStatus value) {
+  return (value | SUCCESS);
+}
+
 bool testEnumeratorCompare() {
   return SUCCESS;
 }
@@ -104,4 +112,16 @@ bool testIgnored(IgnoredSecondEnum value) {
   return value;
 }
 
+enum CustomOperatorEnum {
+    E0 = 0x1,
+    E1 = 0x2,
+    E2 = 0x4
+};
+
+CustomOperatorEnum operator&(CustomOperatorEnum a, CustomOperatorEnum b) { return static_cast<CustomOperatorEnum>(a & b); }
+
+void testCustomOperator(CustomOperatorEnum e) {
+    if (e & E1) {}
+}
+
 }


        


More information about the cfe-commits mailing list