STL: added testcase for 'if (str.find(%any%)) ..'

This commit is contained in:
Daniel Marjamäki 2010-05-01 21:56:39 +02:00
parent ae576be088
commit 136b84eaf5
2 changed files with 13 additions and 2 deletions

View File

@ -553,6 +553,8 @@ void CheckStl::if_find()
// stl container
if (Token::Match(decl, "const| std :: %var% < %type% > &|*| %varid%", varid))
if_findError(tok, false);
else if (Token::Match(decl, "const| std :: string &|*| %varid%", varid))
if_findError(tok, true);
}
}
@ -576,7 +578,7 @@ void CheckStl::if_find()
void CheckStl::if_findError(const Token *tok, bool str)
{
if (str)
reportError(tok, Severity::possibleStyle, "stlIfStrFind", "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.");
reportError(tok, Severity::style, "stlIfStrFind", "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.");
else
reportError(tok, Severity::style, "stlIfFind", "Suspicious condition. The result of find is an iterator, but it is not properly checked.");
}

View File

@ -76,6 +76,7 @@ private:
// if (str.find("ab"))
TEST_CASE(if_find);
TEST_CASE(if_str_find);
TEST_CASE(size1);
}
@ -736,7 +737,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void if_str_find()
{
// error
check("void f(const std::string &s)\n"
"{\n"
" if (s.find(\"abc\")) { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (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());
}
void size1()