Fixed false positive #4039: Handle operator precedence in CheckStl::size()

This commit is contained in:
PKEuS 2012-08-25 12:36:13 +02:00
parent 985ac662ee
commit bb068d2f78
2 changed files with 8 additions and 2 deletions

View File

@ -926,8 +926,8 @@ void CheckStl::size()
if (varid) {
// check for comparison to zero
if (Token::Match(end, "==|<=|!=|> 0") ||
Token::Match(tok->tokAt(-2), "0 ==|>=|!=|<")) {
if ((tok->previous() && !tok->previous()->isArithmeticalOp() && Token::Match(end, "==|<=|!=|> 0")) ||
(end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "0 ==|>=|!=|<"))) {
if (isStlContainer(varid))
sizeError(tok1);
}

View File

@ -1449,6 +1449,12 @@ private:
" fun(a && x.size());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (performance) Possible inefficient checking for 'x' emptiness.\n", errout.str());
check("void f() {\n" // #4039
" std::list<int> x;\n"
" fun(width % x.size() != 0);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void redundantCondition1() {