Menu

False positive when using C++17 if-statement with initializer

2019-06-12
2019-06-13
  • Haakon Riiser

    Haakon Riiser - 2019-06-12

    I am new to cppcheck, and was surprised to find that this code generates a warning:

    #include <map>
    
    int main()
    {
        std::map<char const*, int> m{ {"a", 1} };
        if (auto iter = m.find("x"); iter != m.end()) {
            return iter->second;
        }
        return 0;
    }
    

    To reproduce the warning, use cppcheck --enable=all --std=c++17 <filename>.cpp. Using the latest git head (2a4b28c267059b5520f47f528a67f903d910f2df), I get:

    [if-init.cpp:6]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.
    

    I tried searching for it on trac.cppcheck.net, but couldn't find anything, so I thought I should mention it here.

     
  • Daniel Marjamäki

    Thanks for reporting this bug! I created this ticket: https://trac.cppcheck.net/ticket/9176

     
  • Haakon Riiser

    Haakon Riiser - 2019-06-13

    👍

    I have another potential ticket for you, but I'll create a new thread for it since it is completely unrelated to this one. :)

     

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.