Menu

False positive [stlIfFind]

2020-02-04
2020-02-10
  • Pascal Muller

    Pascal Muller - 2020-02-04

    When using cppcheck on the test program below, I get the following warning:
    (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked. [stlIfFind]

    At the concerning line I'm using a C++17 feature (If statement with initializer); and that seems to be causing the false positive. In the test program I also tried to separate the initializing condition and the if; this causes no warning. (I marked both the place I get the warning and the place I get no warning in the comments in the source)

    I'm using cppcheck 1.90 and set the C++ standard to C++17 within cppcheck. Cppcheck seems to understand the syntax: setting this lower than C++17 causes a syntax error.

    The following program demonstrates the false positive:

    #include <map>
    
    int main()
    {
        std::map<int, int> testMap;
    
        if (auto result = testMap.find(0); result != testMap.end()) // -> Warning: "Suspicious condition. The result of find() is an iterator, but it is not properly checked."
        {
            return 1;
        }
    
        auto result = testMap.find(1);
        if (result != testMap.end())  // -> No warning
        {
            return 2;
        }
    
        return 0;
    }
    

    I attached this piece of code as an attachment for your convenience.

    I believe that both if statements should NOT get this warning. I believe that in the upper case the behavior is incorrect.

     

    Last edit: Pascal Muller 2020-02-06
  • Daniel Marjamäki

    Thanks! I totally agree. I created this ticket: https://trac.cppcheck.net/ticket/9630

     

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.