Menu

FP: knownConditionTrueFalse for assert check

2025-12-05
2025-12-05
  • Marcin Pytel

    Marcin Pytel - 2025-12-05

    I'm using cppcheck 2.18.0

    Here is code:

    static const int i = 0;
    int main()
    {
        assert(i == 0);
    }
    

    When running cppcheck --enable=style I have issue:

    cppcheckbug_4:4:10: style: The comparison 'i == 0' is always true. [knownConditionTrueFalse]
    assert(i == 0);
             ^
    cppcheckbug_4:1:22: note: 'i' is assigned value '0' here.
    static const int i = 0;
                         ^
    cppcheckbug_4:4:10: note: The comparison 'i == 0' is always true.
    assert(i == 0);
    

    Although the cppcheck has right, the assert is to make sure the variable is 0 and e.g. no one will change it to other value because code below the assert works only for 0. That's the idea of assert and the issue shouldn't be raised here.

     
  • CHR

    CHR - 2025-12-05

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14315

     

Log in to post a comment.