Skip to content

Commit 332be17

Browse files
authored
[clang-tidy]fix readability-implicit-bool-conversion false-positives when comparison bool bitfield (#77878)
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 during matching targeted AST.
1 parent 7c77355 commit 332be17

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
274274
allOf(anyOf(hasCastKind(CK_NullToPointer),
275275
hasCastKind(CK_NullToMemberPointer)),
276276
hasSourceExpression(cxxBoolLiteral()))),
277-
hasSourceExpression(expr(hasType(booleanType()))),
278-
unless(ExceptionCases));
277+
hasSourceExpression(expr(hasType(booleanType()))));
279278
auto BoolXor =
280279
binaryOperator(hasOperatorName("^"), hasLHS(ImplicitCastFromBool),
281280
hasRHS(ImplicitCastFromBool));
@@ -315,7 +314,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
315314
traverse(
316315
TK_AsIs,
317316
implicitCastExpr(
318-
ImplicitCastFromBool,
317+
ImplicitCastFromBool, unless(ExceptionCases),
319318
// Exclude comparisons of bools, as they are always cast to
320319
// integers in such context:
321320
// bool_expr_a == bool_expr_b

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ Changes in existing checks
493493
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
494494
do-while loops into account for the `AllowIntegerConditions` and
495495
`AllowPointerConditions` options. It also now provides more consistent
496-
suggestions when parentheses are added to the return value.
496+
suggestions when parentheses are added to the return value. It also ignores
497+
false-positives for comparison containing bool bitfield.
497498

498499
- Improved :doc:`readability-misleading-indentation
499500
<clang-tidy/checks/readability/misleading-indentation>` check to ignore

clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-allow-in-conditions.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int* functionReturningPointer();
1212
struct Struct {
1313
int member;
1414
unsigned bitfield : 1;
15+
bool boolfield : 1;
1516
};
1617

1718

@@ -28,6 +29,8 @@ void implicitConversionIntegerToBoolInConditionalsIsAllowed() {
2829
if (!s.member) {}
2930
if (s.bitfield) {}
3031
if (!s.bitfield) {}
32+
if (s.boolfield == true) {}
33+
if (s.boolfield != true) {}
3134
if (functionReturningInt()) {}
3235
if (!functionReturningInt()) {}
3336
if (functionReturningInt() && functionReturningPointer()) {}

0 commit comments

Comments
 (0)