Skip to content

Create a check to look for bitwise operators used on boolean operands #40307

Open
@LegalizeAdulthood

Description

Bugzilla Link 40962
Version unspecified
OS All

Extended Description

Sometimes people write code like this:

bool invalid = false;
invalid |= x > limit.x;
invalid |= y > limit.y;
invalid |= z > limit.z;
if (invalid) {
  // error handling
}

However, we don't get short circuit evaluation in this case; all limits are checked even if the first limit is violated.

The alternative is to use logical operations instead of bitwise operations:

bool invalid = false;
invalid = x > limit.x;
invalid = invalid || y > limit.x;
invalid = invalid || z > limit.z;
if (invalid) {
  // error handling
}

Create a check that transforms bitwise operators on boolean quantities to logical operators.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillacheck-requestRequest for a new check in clang-tidyclang-tidy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions