Menu

false positive: knwoConditionTrueFalse

CHR
2021-01-07
2021-01-07
  • CHR

    CHR - 2021-01-07
     #include <vector>
    
     int test(const std::vector<int>& Arr) {
         if (!Arr.empty() && Arr[0] == 0)
             return 2;
         if (!Arr.empty())
             return Arr[0];
         return 1;
     }
    

    v.2.3 called with "C:\Program Files\Cppcheck\cppcheck.exe" --inconclusive --enable=all foo.cpp reports

    Checking foo.cpp ...
    foo.cpp:30:7: style: Condition '!Arr.empty()' is always false [knownConditionTrueFalse]
      if (!Arr.empty())
          ^
    foo.cpp:28:17: note: Assuming that condition 'Arr.empty()' is not redundant
      if (!Arr.empty() && Arr[0] == 0)
                    ^
    foo.cpp:30:7: note: Condition '!Arr.empty()' is always false
      if (!Arr.empty())
          ^
    foo.cpp:13:0: style: The function 'nullArith' is never used. [unusedFunction]
    

    The warning disappears if const is removed from test()'s signature.

     
  • Daniel Marjamäki

    Thanks! I have created ticket https://trac.cppcheck.net/ticket/10088

     

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.