STL: suspicious conditions when using find. Added ok testcases and assert that no error message is written

This commit is contained in:
Daniel Marjamäki 2010-02-27 21:35:33 +01:00
parent 4de700c9de
commit 69d6453ea8
1 changed files with 25 additions and 0 deletions

View File

@ -646,17 +646,42 @@ private:
void if_find()
{
// ---------------------------
// string::find
// ---------------------------
// error
check("void f(std::string s)\n"
"{\n"
" if (s.find(\"ab\")) { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (possible style) Suspicious condition. string::find will return 0 if the string is found at position 0. If this is what you want to check then string::compare is a faster alternative because it doesn't scan through the string.\n", errout.str());
// ok
check("void f(std::string s)\n"
"{\n"
" if (s.find(\"ab\") != std::string::npos) { }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// ---------------------------
// set::find
// ---------------------------
// error
check("void f(std::set<int> s)\n"
"{\n"
" if (s.find(12)) { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Suspicious condition. The result of find is an iterator, but it is not properly checked.\n", errout.str());
// ok
check("void f(std::set<int> s)\n"
"{\n"
" if (s.find(123) != s.end()) { }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}