[clang-tools-extra] [clang-tidy] Exclude bitwise operators in bugprone-non-zero-enum-to-bool-conversion (PR #65498)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 7 10:49:38 PDT 2023
https://github.com/PiotrZSL updated https://github.com/llvm/llvm-project/pull/65498:
>From 79b61b63896a203793bdb0af62c8bee4f676a5c1 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Wed, 6 Sep 2023 16:41:22 +0000
Subject: [PATCH 1/2] [clang-tidy] Exclude bitwise operators in
bugprone-non-zero-enum-to-bool-conversion
Improved bugprone-non-zero-enum-to-bool-conversion check by
eliminating false positives resulting from direct usage of
bitwise operators.
---
.../NonZeroEnumToBoolConversionCheck.cpp | 6 +++++-
clang-tools-extra/docs/ReleaseNotes.rst | 6 +++++-
.../non-zero-enum-to-bool-conversion.cpp | 20 +++++++++++++++++++
3 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
index a59c4fc47c0b4e9..8a945ac65cfc9b2 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
@@ -49,6 +49,9 @@ bool NonZeroEnumToBoolConversionCheck::isLanguageVersionSupported(
}
void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
+ auto ExcludedOperators = binaryOperation(hasAnyOperatorName(
+ "|", "&", "^", "<<", ">>", "~", "|=", "&=", "^=", "<<=", ">>="));
+
Finder->addMatcher(
castExpr(hasCastKind(CK_IntegralToBoolean),
unless(isExpansionInSystemHeader()), hasType(booleanType()),
@@ -58,7 +61,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 a2cde526a8c04d9..0bccc0b4023eaa7 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.
@@ -227,7 +231,7 @@ Changes in existing checks
`DeduplicateFindings` to output one finding per symbol occurrence.
- Improved :doc:`misc-include-cleaner
- <clang-tidy/checks/misc/include-cleaner>` check to avoid fixes insert
+ <clang-tidy/checks/misc/include-cleaner>` check to avoid fixes insert
same include header multiple times.
- Improved :doc:`misc-redundant-expression
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) {}
+}
+
}
>From fa6fc6bd7bb168e8664b7c347973e03ef33c3ca8 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Thu, 7 Sep 2023 17:36:29 +0000
Subject: [PATCH 2/2] Add comment
---
.../clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
index 8a945ac65cfc9b2..918b6e3824f0b1b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
@@ -49,6 +49,8 @@ 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(
"|", "&", "^", "<<", ">>", "~", "|=", "&=", "^=", "<<=", ">>="));
More information about the cfe-commits
mailing list