Fixed #4305 (improve check: 'vector.size() < 1' should result in 'inefficient checking for '...' emptiness.')

This commit is contained in:
Andrew Martin 2012-11-04 16:15:26 +01:00 committed by Daniel Marjamäki
parent dc3eba964b
commit 7c370ec873
2 changed files with 35 additions and 0 deletions

View File

@ -954,6 +954,13 @@ void CheckStl::size()
sizeError(tok1);
}
// check for comparison to one
if ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, ">=|< 1")) ||
(end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "1 <=|>"))) {
if (isStlContainer(varid))
sizeError(tok1);
}
// check for using as boolean expression
else if ((Token::Match(tok->tokAt(-2), "if|while (") && end->str() == ")") ||
(tok->previous()->type() == Token::eLogicalOp && Token::Match(end, "&&|)|,|;|%oror%"))) {

View File

@ -1441,6 +1441,34 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::list<int> x;\n"
" if (x.size() >= 1) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::list<int> x;\n"
" if (x.size() < 1) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f()\n"
"{\n"
" std::list<int> x;\n"
" if (1 <= 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::list<int> x;\n"
" if (1 > 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::list<int> x;\n"