Skip to content

Commit

Permalink
[clang-tidy]fix readability-implicit-bool-conversion false-positives …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
HerrCai0907 committed Jan 15, 2024
1 parent 7c77355 commit 332be17
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ int* functionReturningPointer();
struct Struct {
int member;
unsigned bitfield : 1;
bool boolfield : 1;
};


Expand All @@ -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()) {}
Expand Down

0 comments on commit 332be17

Please sign in to comment.