Menu

Why compareBoolExpressionWithInt?

2019-08-29
2020-09-06
  • Sander Bouwhuis

    Sander Bouwhuis - 2019-08-29

    I have the following code (m_bEnabled is a bool):

    1. case WM_ENABLE: // 0x00a
    2.   // Is the state changing?
    3.   if((wParam == 0 ? false : true) != m_bEnabled)
    4.   {
    5.     // Change the state
    6.     m_bEnabled = !m_bEnabled;
    7.   }
    8.   break;
    

    CppCheck v1.88 gives error compareBoolExpressionWithInt on line 3.
    Why is that? The wParam gets compared to an int (0), after which everything between the round brackets becomes either 'false' or 'true'. Then that gets compared to m_bEnabled, which is a bool.

    Why does this warning occur?

     

    Last edit: Sander Bouwhuis 2019-08-29
  • versat

    versat - 2019-08-29

    With the following simplified C++ code i can reproduce it with 1.89 dev:

    bool f(int a, bool b)
    {
        if ((a == 0 ? false : true) != b) {
            b = !b;
        }
        return b;
    }
    

    Output:

    $ g++ -fsyntax-only fp_compareBoolExpressionWithInt.cpp && ./cppcheck --enable=warning --debug fp_compareBoolExpressionWithInt.cpp
    Checking fp_compareBoolExpressionWithInt.cpp ...
    
    ##file fp_compareBoolExpressionWithInt.cpp
    1: bool f ( int a@1 , bool b@2 )
    2: {
    3: if ( ( a@1 == 0 ? false : true ) != b@2 ) {
    4: b@2 = ! b@2 ;
    5: }
    6: return b@2 ;
    7: }
    
    ##Value flow
    Line 3
      a possible 0
      == possible 1
      0 always 0
      ? possible {0,1}
      false always 0
      : always 1
      true always 1
    fp_compareBoolExpressionWithInt.cpp:3:33: warning: Comparison of a boolean expression with an integer. [compareBoolExpressionWithInt]
        if ((a == 0 ? false : true) != b) {
                                    ^
    

    Looks like a false positive to me.
    If i am not missing anything this warning should not be shown.

     

    Last edit: versat 2019-08-29
  • versat

    versat - 2019-08-29

    I have created a ticket for this: https://trac.cppcheck.net/ticket/9304

     
  • Sander Bouwhuis

    Sander Bouwhuis - 2019-08-29

    Thanks. It's always good news when false positives can be fixed.

    I rewrote it to this:

    if((wParam != 0) != m_bEnabled)
    

    Now CppCheck doesn't fail anymore.

     
  • Ken-Patrick Lehrmann

    This issue should be fixed with https://github.com/danmar/cppcheck/pull/2131

     

    Last edit: Ken-Patrick Lehrmann 2019-08-31
  • Fabio

    Fabio - 2020-09-06

    Just encountered this issue, but on a different type of code.

    #include <variant>
    
    template <typename Variant>
    auto vcheck1() {
        return std::variant_size_v<Variant>-1; // <- this one has the issue
    }
    
    template <typename Variant>
    auto vcheck2() {
        return std::variant_size<Variant>::value-1; // <- this one has not
    }
    

    Problem exists in trunk.

     

    Last edit: Fabio 2020-09-06

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.