Skip to content

[clang-tidy] Exclude bitwise operators in bugprone-non-zero-enum-to-bool-conversion #65498

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

PiotrZSL
Copy link
Member

@PiotrZSL PiotrZSL commented Sep 6, 2023

Improved bugprone-non-zero-enum-to-bool-conversion check by eliminating false positives resulting from direct usage of bitwise operators.

…ool-conversion

Improved bugprone-non-zero-enum-to-bool-conversion check by
eliminating false positives resulting from direct usage of
bitwise operators.
@HerrCai0907
Copy link
Contributor

explicit bit usage will not case false positive without this patch. Should we consider case of operator overloading?

bool explicitBitUsage1(EStatus value) {
  return (value & SUCCESS);
}
bool explicitBitUsage2(EStatus value) {
  return (value | SUCCESS);
}

@PiotrZSL
Copy link
Member Author

PiotrZSL commented Sep 7, 2023

explicit bit usage will not case false positive without this patch. Should we consider case of operator overloading?

Yes, thats a side effect due to implicit casting enums to integers. I added tests because those were missing, then I implemented version with cxxOverloadCallExpr and changed it into more generic version by using binaryOperation.
If you want, then yes, I can reduce this back to overload operators calls only.

@@ -49,6 +49,9 @@ bool NonZeroEnumToBoolConversionCheck::isLanguageVersionSupported(
}

void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
auto ExcludedOperators = binaryOperation(hasAnyOperatorName(
"|", "&", "^", "<<", ">>", "~", "|=", "&=", "^=", "<<=", ">>="));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want, then yes, I can reduce this back to overload operators calls only.

I think current implement it fine, but maybe add some comment here to clarify why you want to exclude those operators. Because actually it's for the operator overloading instead of explicit operator.

@PiotrZSL PiotrZSL merged commit 503e315 into llvm:main Sep 10, 2023
@PiotrZSL PiotrZSL deleted the 65315-clang-tidy-false-positive-bugprone-non-zero-enum-to-bool-conversion-with-custom-operator branch September 10, 2023 16:09
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…ool-conversion (llvm#65498)

Improved bugprone-non-zero-enum-to-bool-conversion check by eliminating
false positives resulting from direct usage of bitwise operators.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] False positive bugprone-non-zero-enum-to-bool-conversion with custom operator
2 participants