Menu

"Condition is always false" false positive in Cppcheck v2.5

Alan
2021-07-07
2021-07-09
  • Alan

    Alan - 2021-07-07

    Cppcheck will throw this style error on this section of code:
    "Condition '(test_states^(FIRST|SECOND|THIRD|FOURTH))==0' is always false"

    I think this is a false positive, because executed_states could be equal to ( FIRST | SECOND | THIRD | FOURTH), in which case the condition Cppcheck is complaining about would be true.

    This is a new error that popped up for me after upgrading from version 2.4 to 2.5

    enum states {  FIRST = 1,
                      SECOND = 2,
                      THIRD = 4,
                      FOURTH = 8,
                      COMPLETE = 0x10};
    
    
    void Foo::function(unsigned int executed_states)
    {
        if(!(executed_states & COMPLETE))
        {
    
            unsigned int test_states = executed_states & ( FIRST | SECOND | THIRD | FOURTH);
    
            if((test_states ^ (FIRST | SECOND | THIRD | FOURTH)) == 0)
            {
                cout << "here" << endl;
            }
        }
        else
        {
            cout << "complete" << endl;
        }
    
        return;
    }
    
     
  • CHR

    CHR - 2021-07-09

    Thanks, I can reproduce this with current head. Ticket is here.

     

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.