Menu

False positive - knownConditionTrueFalse

pokopa2803
2020-09-30
2020-10-01
  • pokopa2803

    pokopa2803 - 2020-09-30

    Cppcheck-2.1

    #include <iostream>
    #include <stdlib.h>
    #include <map>
    
    bool True(int& number)
    {
       number = rand();
       return number > 5;
    }
    
    void TestFunction(const std::string& fileName)
    {
       std::map<std::string, int> filesSizeMap;
    
       std::ifstream file(fileName.c_str());
       if (file)
       {
          std::string temp;
          while (getline(file, temp))
          {
             if (temp[0] != 'x')
             {
                int someNumber = 0;
                if (True(someNumber))
                {
                   filesSizeMap[temp] = someNumber;
                }
             }
          }
       }
       else
       {
           std::cout << "test_cppcheck";
       }
    
       if (!filesSizeMap.empty())
       {
         std::cout << "cppcheck";
       }
    }
    

    It reports "style/knownConditionTrueFalse: Condition '!filesSizeMap.empty()' is always false"

     

    Last edit: pokopa2803 2020-09-30
  • Daniel Marjamäki

    Thanks! I hope we have fixed this. I can reproduce the false positive with cppcheck-2.0 but I can't reproduce with cppcheck HEAD. cppcheck-2.2 will be released soon.

     

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.