diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index a5f87bb63..967288433 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -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; } diff --git a/test/teststl.cpp b/test/teststl.cpp index e1c84b675..9a3c41eb0 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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 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 s)\n" "{\n" @@ -1539,6 +1546,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + // ok (assignment) + check("void f(std::set s)\n" + "{\n" + " if (a || (x = s.find(12)) != s.end()) { }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // --------------------------- // std::find