Improve 'Use x.empty() instead of x.size() for emptiness' warning.
Improve the performance warning message as discussed at dev-forum: https://sourceforge.net/apps/phpbb/cppcheck/viewtopic.php?f=3&t=192#p926
This commit is contained in:
parent
8369d0ddd6
commit
d14b5039ce
|
@ -807,12 +807,13 @@ void CheckStl::size()
|
||||||
void CheckStl::sizeError(const Token *tok)
|
void CheckStl::sizeError(const Token *tok)
|
||||||
{
|
{
|
||||||
const std::string varname(tok ? tok->str().c_str() : "list");
|
const std::string varname(tok ? tok->str().c_str() : "list");
|
||||||
const bool verbose(_settings ? _settings->_verbose : true);
|
reportError(tok, Severity::performance, "stlSize",
|
||||||
reportError(tok, Severity::performance, "stlSize", "Use " + varname + ".empty() instead of " + varname + ".size() to guarantee fast code." + (verbose ? " size() can take linear time but empty() is guaranteed to take constant time." : ""));
|
"Possible inefficient checking for '" + varname + "' emptiness.\n"
|
||||||
|
"Using " + varname + ".empty() instead of " + varname + ".size() can be faster. " +
|
||||||
|
varname + ".size() can take linear time but " + varname + ".empty() is "
|
||||||
|
"guaranteed to take constant time.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void CheckStl::redundantCondition()
|
void CheckStl::redundantCondition()
|
||||||
{
|
{
|
||||||
const char pattern[] = "if ( %var% . find ( %any% ) != %var% . end ( ) ) "
|
const char pattern[] = "if ( %var% . find ( %any% ) != %var% . end ( ) ) "
|
||||||
|
|
|
@ -1008,28 +1008,28 @@ private:
|
||||||
" std::list<int> x;\n"
|
" std::list<int> x;\n"
|
||||||
" if (x.size() == 0) {}\n"
|
" if (x.size() == 0) {}\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (performance) Use x.empty() instead of x.size() to guarantee fast code.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" std::list<int> x;\n"
|
" std::list<int> x;\n"
|
||||||
" if (x.size() != 0) {}\n"
|
" if (x.size() != 0) {}\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (performance) Use x.empty() instead of x.size() to guarantee fast code.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" std::list<int> x;\n"
|
" std::list<int> x;\n"
|
||||||
" if (x.size() > 0) {}\n"
|
" if (x.size() > 0) {}\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (performance) Use x.empty() instead of x.size() to guarantee fast code.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" std::list<int> x;\n"
|
" std::list<int> x;\n"
|
||||||
" if (x.size()) {}\n"
|
" if (x.size()) {}\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (performance) Use x.empty() instead of x.size() to guarantee fast code.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
|
||||||
|
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
|
Loading…
Reference in New Issue