Skip to content

[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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
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