-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy]fix readability-implicit-bool-conversion false-positives when comparison bool bitfield #77878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…s when comparison bool bitfield Fixes: #76817 For ignoring comparison and xor operator, it needs to use ImplicitCastFromBool without ignoring exception cases. This patch splits ignoring exception cases logic from ImplicitCastFromBool and only applies it when add matcher
@llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) ChangesFixes: #76817 Full diff: https://github.com/llvm/llvm-project/pull/77878.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index f0fca30de3b3c4..672f28721114c9 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -274,8 +274,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
allOf(anyOf(hasCastKind(CK_NullToPointer),
hasCastKind(CK_NullToMemberPointer)),
hasSourceExpression(cxxBoolLiteral()))),
- hasSourceExpression(expr(hasType(booleanType()))),
- unless(ExceptionCases));
+ hasSourceExpression(expr(hasType(booleanType()))));
auto BoolXor =
binaryOperator(hasOperatorName("^"), hasLHS(ImplicitCastFromBool),
hasRHS(ImplicitCastFromBool));
@@ -315,7 +314,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
traverse(
TK_AsIs,
implicitCastExpr(
- ImplicitCastFromBool,
+ ImplicitCastFromBool, unless(ExceptionCases),
// Exclude comparisons of bools, as they are always cast to
// integers in such context:
// bool_expr_a == bool_expr_b
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index b4d87e0ed2a67a..f7274229bf52ce 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -478,7 +478,8 @@ Changes in existing checks
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
do-while loops into account for the `AllowIntegerConditions` and
`AllowPointerConditions` options. It also now provides more consistent
- suggestions when parentheses are added to the return value.
+ suggestions when parentheses are added to the return value. It also ignores
+ false-positives for comparison containing bool bitfield.
- Improved :doc:`readability-misleading-indentation
<clang-tidy/checks/readability/misleading-indentation>` check to ignore
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-allow-in-conditions.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-allow-in-conditions.cpp
index e393e297140cbd..48984d29322872 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-allow-in-conditions.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-allow-in-conditions.cpp
@@ -12,6 +12,7 @@ int* functionReturningPointer();
struct Struct {
int member;
unsigned bitfield : 1;
+ bool boolfield : 1;
};
@@ -28,6 +29,8 @@ void implicitConversionIntegerToBoolInConditionalsIsAllowed() {
if (!s.member) {}
if (s.bitfield) {}
if (!s.bitfield) {}
+ if (s.boolfield == true) {}
+ if (s.boolfield != true) {}
if (functionReturningInt()) {}
if (!functionReturningInt()) {}
if (functionReturningInt() && functionReturningPointer()) {}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but please note that I didn't get too deep into current ast matching, and movement of ExceptionCases higher looks a little bit suspicious. But I assume also that current tests cover this deeply.
…when comparison bool bitfield (llvm#77878) Fixes: llvm#76817 For ignoring comparison and xor operator, it needs to use `ImplicitCastFromBool` without ignoring exception cases. This patch splits ignoring exception cases logic from `ImplicitCastFromBool` and only applies it during matching targeted AST.
Fixes: #76817
For ignoring comparison and xor operator, it needs to use ImplicitCastFromBool without ignoring exception cases. This patch splits ignoring exception cases logic from ImplicitCastFromBool and only applies it when add matcher