Skip to content

[clang-tidy] False positive bugprone-non-zero-enum-to-bool-conversion with custom operator and negation #81515

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

Closed
chrchr-github opened this issue Feb 12, 2024 · 1 comment · Fixed by #81890
Assignees
Labels
clang-tidy confirmed Verified by a second party false-positive Warning fires when it should not

Comments

@chrchr-github
Copy link

A slight variation of #65315

enum E { E1 = 1, E2 = 2 };
E operator&(E a, E b) { return static_cast<E>((int)a & (int)b); }
void f(E e) {
    if (!(e & E1)) {}
}
[<source>:4:10: warning: conversion of 'E' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
    4 |     if (!(e & E1)) {}
      |          ^
[<source>:1:6: note: enum is defined here](javascript:;)
    1 | enum E { E1 = 1, E2 = 2 };
      |      ^

https://godbolt.org/z/r1P3zx38a

@EugeneZelenko EugeneZelenko added the false-positive Warning fires when it should not label Feb 12, 2024
@PiotrZSL PiotrZSL added the confirmed Verified by a second party label Feb 12, 2024
@PiotrZSL
Copy link
Member

Problem is caused by extra ().
Fix:

--- a/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NonZeroEnumToBoolConversionCheck.cpp
@@ -64,7 +64,7 @@ void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
                                          EnumIgnoreList)))
                                 .bind("enum"))))),
                         unless(declRefExpr(to(enumConstantDecl()))),
-                        unless(ignoringImplicit(ExcludedOperators)))),
+                        unless(ignoringParenImpCasts(ExcludedOperators)))),
                unless(hasAncestor(staticAssertDecl())))
           .bind("cast"),
       this);

@PiotrZSL PiotrZSL self-assigned this Feb 15, 2024
PiotrZSL added a commit to nokia/llvm-project that referenced this issue Feb 15, 2024
…-bool-conversion

Properly ignore parentheses in bitwise operators.

Closes: llvm#81515
PiotrZSL added a commit that referenced this issue Feb 17, 2024
…-bool-conversion (#81890)

Properly ignore parentheses in bitwise operators.

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