This commit is contained in:
Robert Reif 2011-04-28 19:08:18 -04:00
parent 8aafe2c038
commit a2938b7212
2 changed files with 186 additions and 1 deletions

View File

@ -827,7 +827,7 @@ bool CheckStl::isStlContainer(unsigned int varid)
type = type->next()->next(); type = type->next()->next();
// all possible stl containers // all possible stl containers
static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|vector"; static const char STL_CONTAINER_LIST[] = "basic_string|bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|vector";
// container template string // container template string
const std::string checkStr(std::string(STL_CONTAINER_LIST) + " <"); const std::string checkStr(std::string(STL_CONTAINER_LIST) + " <");
@ -835,6 +835,9 @@ bool CheckStl::isStlContainer(unsigned int varid)
// check if it's an stl template // check if it's an stl template
if (Token::Match(type, checkStr.c_str())) if (Token::Match(type, checkStr.c_str()))
return true; return true;
if (Token::Match(type, "string|wstring"))
return true;
} }
return false; return false;

View File

@ -91,6 +91,8 @@ private:
TEST_CASE(size1); TEST_CASE(size1);
TEST_CASE(size2); TEST_CASE(size2);
TEST_CASE(size3); // ticket #2757
TEST_CASE(size4); // ticket #2757
// Redundant conditions.. // Redundant conditions..
// if (ints.find(123) != ints.end()) ints.remove(123); // if (ints.find(123) != ints.end()) ints.remove(123);
@ -1179,6 +1181,186 @@ private:
ASSERT_EQUALS("[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str()); ASSERT_EQUALS("[test.cpp:10]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
} }
void size3() // ticket #2757
{
check("struct Fred {\n"
" void foo();\n"
" std::basic_string<TCHAR> x;\n"
"};\n"
"void Fred::foo()\n"
"{\n"
" if (x.size() == 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("std::basic_string<TCHAR> x;\n"
"void f()\n"
"{\n"
" if (x.size() == 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (x.size() == 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (0 == x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (x.size() != 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (0 != x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (x.size() > 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (0 < x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" if (!x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" fun(x.size());\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" std::basic_string<TCHAR> x;\n"
" fun(!x.size());\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void size4() // ticket #2757
{
check("struct Fred {\n"
" void foo();\n"
" std::string x;\n"
"};\n"
"void Fred::foo()\n"
"{\n"
" if (x.size() == 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("std::string x;\n"
"void f()\n"
"{\n"
" if (x.size() == 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (x.size() == 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (0 == x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (x.size() != 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (0 != x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (x.size() > 0) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (0 < x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" if (!x.size()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" fun(x.size());\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" std::string x;\n"
" fun(!x.size());\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void redundantCondition2() void redundantCondition2()
{ {
check("void f()\n" check("void f()\n"