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>intmain()
{
std::map<int, int>testMap;if(autoresult=testMap.find(0); result != testMap.end()) // -> Warning: "Suspicious condition. The result of find() is an iterator, but it is not properly checked."
{
return1;
}
autoresult=testMap.find(1);if(result!=testMap.end())//->Nowarning
{
return2;
}
return0;
}
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.
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:
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
Thanks! I totally agree. I created this ticket: https://trac.cppcheck.net/ticket/9630