Menu

Unchecked iterator false positives with C++17 if initializer

2019-07-26
2019-07-27
  • Nikos Chantziaras

    (Not sure how to register on Trac, so I'm posting this bug report here.)

    I started a new project and I'm free to require C++17, so am using all language features at will without restrictions. But I ran into an annoying false positive that is guaranteed to occur every time I do something similar to this:

    if (auto it = items.find(key); it != items.end()) {
        // can safely use 'it'
    } else {
        // not found
    }
    

    cppcheck 1.88 always wrongly reports:

    Suspicious condition. The result of find() is an iterator, but is not properly checked.

    The non-initializer equivalent of the above code does not generate the false positive.

     
  • Daniel Marjamäki

    Thanks! I created this ticket: https://trac.cppcheck.net/ticket/9237

     

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.