Menu

Missed always true situation

2021-10-12
2021-10-14
  • Steve Albright

    Steve Albright - 2021-10-12

    I was told clazy caught this, tried with cppcheck 2.6 and it passed so there might be an opportunity to make the tool better.

    I checked and confirmed that enum1 and enum2 were not equal so therefore mode can't be equal to both so the or is always true.

    if((mode != enum1 || mode != enum2) && !SomeBooleanFunction())
    
     
  • Daniel Marjamäki

    can you please provide a proper example code. with this code I get a warning:

    enum class Mode { enum1, enum2 };
    
    void foo(Mode mode) {
        if((mode != enum1 || mode != enum2) && !SomeBooleanFunction()) {}
    }
    
     
    • Daniel Marjamäki

      1.cpp:5:23: warning: Logical disjunction always evaluates to true: mode != 0 || mode != 1. [incorrectLogicOperator]
          if((mode != enum1 || mode != enum2) && !SomeBooleanFunction()) {}
                            ^
      
       
  • Steve Albright

    Steve Albright - 2021-10-13

    OK, this was hard to reproduce but I finally did but it required me to put the enum definition in a different header file instead (which happens to be referenced as ProjectName/HeaderFile.h in the cpp file and is in a completely different location so this could be a case of cppcheck not finding the path that we have managed with CMake to keep our source compiling.

    The Header (say path is sourceDir/OtherFolder/ProjectName/Example.h)

    enum ExampleEnum
    {
       val1 = 0,
       val2,
       val3
    };
    

    The CPP The Header (say path is sourceDir/ProjectName/Example.cpp)

    void Example(ExampleEnum exampleEnumValue)
    {
       if(exampleEnumValue != val1 || exampleEnumValue != val3)
       {
          // clazy knows the paths better and gives the warning that there are overlapping conditions so there is an always true
       }
    }
    

    Maybe I need to figure out how to set paths better in CPPCheck or it could/should scan cmake better?

     
  • Daniel Marjamäki

    yes it sounds like a include path problem.. does it work better if you generate and load a compile database. cppcheck --project=compile_commands.json

     
    • Steve Albright

      Steve Albright - 2021-10-14

      I tried that and it still didn't find this issue but I did find others to look into...

       

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.