Fixed false positive: Support assignments in CheckStl::if_find()

This commit is contained in:
PKEuS 2015-01-03 11:29:13 +01:00
parent 8885ac3eba
commit 1355f49af7
2 changed files with 17 additions and 1 deletions

View File

@ -743,11 +743,13 @@ static bool if_findCompare(const Token * const tokBack)
{
const Token *tok = tokBack->astParent();
if (!tok)
return false;
return true;
if (tok->isComparisonOp())
return true;
if (tok->isArithmeticalOp()) // result is used in some calculation
return true; // TODO: check if there is a comparison of the result somewhere
if (tok->isAssignmentOp())
return if_findCompare(tok); // Go one step upwards in the AST
return false;
}

View File

@ -1497,6 +1497,13 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
// error (assignment)
check("void f(std::set<int> s)\n"
"{\n"
" if (a || (x = s.find(12))) { }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious condition. The result of find() is an iterator, but it is not properly checked.\n", errout.str());
// ok (simple)
check("void f(std::set<int> s)\n"
"{\n"
@ -1539,6 +1546,13 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
// ok (assignment)
check("void f(std::set<int> s)\n"
"{\n"
" if (a || (x = s.find(12)) != s.end()) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
// ---------------------------
// std::find