diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 9adb79daa..5abd58efe 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1596,6 +1596,17 @@ void CheckStl::readingEmptyStlContainer() } else if (tok->str() == "{" && tok->next()->scope()->type == Scope::eLambda) tok = tok->link(); + // function call + if (Token::Match(tok, "!!. %name% (") && !Token::simpleMatch(tok->linkAt(2), ") {")) { + for (std::map::const_iterator it = emptyContainer.begin(); it != emptyContainer.end();) { + const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(it->first); + if (var && (var->isLocal() || var->isArgument())) + ++it; + else + it = emptyContainer.erase(it); + } + } + if (!tok->varId()) continue; diff --git a/test/teststl.cpp b/test/teststl.cpp index b61d56f36..c03207388 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -2998,6 +2998,22 @@ private: "}", true); ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Reading from empty STL container 'v'\n", errout.str()); + // #7449 - nonlocal vector + check("std::vector v;\n" + "void f() {\n" + " v.clear();\n" + " dostuff()\n" + " if (v.empty()) { }\n" + "}", true); + ASSERT_EQUALS("", errout.str()); + + check("std::vector v;\n" + "void f() {\n" + " v.clear();\n" + " if (v.empty()) { }\n" + "}", true); + ASSERT_EQUALS("[test.cpp:4]: (style, inconclusive) Reading from empty STL container 'v'\n", errout.str()); + // #6663 check("void foo() {\n" " std::set container;\n"