Menu

False positive with knownConditionTrueFalse using EXPECT_GT() vs. EXPECT_NE()

2024-05-28
2024-05-29
  • Steve Albright

    Steve Albright - 2024-05-28

    style: knownConditionTrueFalse - Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.

    Code won't fully compile but you should get the idea. The text field resizes on show() so we are testing for the size change.

    This passes

    TEST(SizeChangeDialogTest, SettingOverlayVisibleWillResizeTitleLabelHeight)
    {
       OurQDialogClassWithContentTitle alertOverlayUT();
       const QLabel* titleLabel = alertOverlayUT.findChild<QLabel*>("ContentTitle");
       const int originalHeight = titleLabel->height();
    
       alertOverlayUT.setVisible(true);
    **   EXPECT_NE(titleLabel->height(), originalHeight);**
    }
    

    This does not and the only difference is Greater Than

    TEST(SizeChangeDialogTest, SettingOverlayVisibleWillResizeTitleLabelHeight)
    {
       OurQDialogClassWithContentTitle alertOverlayUT();
       const QLabel* titleLabel = alertOverlayUT.findChild<QLabel*>("ContentTitle");
       const int originalHeight = titleLabel->height();
    
       alertOverlayUT.setVisible(true);
    **   EXPECT_GT(titleLabel->height(), originalHeight);**
    }
    
     
  • CHR

    CHR - 2024-05-29

    I can't reproduce this with the snippet above.

     
    • Steve Albright

      Steve Albright - 2024-05-29

      Hmm, well, it is happening and in multiple places for me.

       
    • Steve Albright

      Steve Albright - 2024-05-29

      Do you have the google test library added - I wasn't getting the issue without it.

       
      • CHR

        CHR - 2024-05-29

        Yes. Can you confirm that you get the warning on just the code above? Probably there is something missing still.

         
        • Steve Albright

          Steve Albright - 2024-05-29

          Definately getting it, went to a completely different repo with the same configuration and tried it there.

          style: knownConditionTrueFalse - Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.

          Our config looks like this

          cppcheck \
              --library=boost \
              --library=googletest \
              --library=lua \
              --library=qt \
              --library=sqlite3 \
              ${cppCheckOptEnable} \
              --inline-suppr \
              ${cppCheckOptSuppress} \
              --verbose \
              --force \
              --xml \
              --xml-version=2 \
              ${cppCheckOptInclude} \
              ${cppCheckOptExclude} \
              --error-exitcode=1 \
              ${cppCheckFiles[@]} \
              2> "${cppCheckOutputFile}"
          

          }

           
  • CHR

    CHR - 2024-05-29

    I just get

    foo.cpp:1:0: style: The function '__SizeChangeDialogTest_SettingOverlayVisibleWillResizeTitleLabelHeight' is never used. [unusedFunction]
    TEST(SizeChangeDialogTest, SettingOverlayVisibleWillResizeTitleLabelHeight)
    ^
    nofile:0:0: information: Active checkers: 178/802 (use --checkers-report=<filename> to see details) [checkersReport]
    
     
    • Steve Albright

      Steve Albright - 2024-05-29

      Hmm, well, I can consistently reproduce it.

      We are probably using older google test - not sure if that matters.

      Extra configuration we are doing too

          -e "all" \
          -s "checkersReport" \
          -s "missingIncludeSystem" \
          -s "missingInclude" \
          -s "normalCheckLevelMaxBranches" \
          -s "returnByReference" \
          -s "syntaxError" \
          -s "unknownMacro" \
          -s "unmatchedSuppression" \
          -s "unusedFunction" \
          -o "${cppCheckOutputFile}" \
      
       
  • CHR

    CHR - 2024-05-29

    Can you test with just one file and a simple command line like cppcheck file.cpp --library=googletest --enable=all?

     
    • Steve Albright

      Steve Albright - 2024-05-29

      Solo file and reproducable in

      Description: Ubuntu 20.04.6 LTS
      Release: 20.04
      Codename: focal

      #include <QLabel>
      #include <gtest/gtest.h>
      
      TEST(SizeChangeDialogTest, SettingOverlayVisibleWillResizeTitleLabelHeight)
      {
         OurQDialogClassWithContentTitle alertOverlayUT();
         const QLabel* titleLabel = alertOverlayUT.findChild<QLabel*>("ContentTitle");
         const int originalHeight = titleLabel->height();
      
         alertOverlayUT.setVisible(true);
         EXPECT_GT(titleLabel->height(), originalHeight);
      }
      

      Command:

      cppcheck-2.14.0/build$ cppcheck ~/workspace/GreaterThanTest.cpp --library=googletest --enable=all

      Output:

      Checking /.../GreaterThanTest.cpp ...
      /home/sa10319/workspace/GreaterThanTest.cpp:1:0: information: Include file: <QLabel> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]
      #include <QLabel>
      ^
      /.../GreaterThanTest.cpp:2:0: information: Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]
      #include <gtest/gtest.h>
      ^
      /.../GreaterThanTest.cpp:11:4: style: The comparison 'titleLabel->height() > originalHeight' is always false because 'titleLabel->height()' and 'originalHeight' represent the same value. [knownConditionTrueFalse]
         EXPECT_GT(titleLabel->height(), originalHeight);
         ^
      /.../GreaterThanTest.cpp:8:49: note: 'originalHeight' is assigned value 'titleLabel->height()' here.
         const int originalHeight = titleLabel->height();
                                                      ^
      /.../GreaterThanTest.cpp:11:4: note: The comparison 'titleLabel->height() > originalHeight' is always false because 'titleLabel->height()' and 'originalHeight' represent the same value.
         EXPECT_GT(titleLabel->height(), originalHeight);
         ^
      /.../GreaterThanTest.cpp:4:0: style: The function '__SizeChangeDialogTest_SettingOverlayVisibleWillResizeTitleLabelHeight' is never used. [unusedFunction]
      TEST(SizeChangeDialogTest, SettingOverlayVisibleWillResizeTitleLabelHeight)
      
       

      Last edit: Steve Albright 2024-05-29
  • CHR

    CHR - 2024-05-29

    On a different machine, I'm suddenly seeing the warning. Ticket is here: https://trac.cppcheck.net/ticket/12795

     

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.