Fix issue 9550: False positive: Same iterator is used with containers 'x' that are defined in different scopes (#2463)

This commit is contained in:
Paul Fultz II 2019-12-31 01:09:04 -06:00 committed by Daniel Marjamäki
parent 7dcfd3400f
commit 82c91f9484
2 changed files with 24 additions and 3 deletions

View File

@ -3151,7 +3151,7 @@ ValueFlow::Value getLifetimeObjValue(const Token *tok)
result = *it;
// There should only be one lifetime
if (std::find_if(std::next(it), tok->values().end(), pred) != tok->values().end())
return result;
return ValueFlow::Value{};
return result;
}
@ -3635,8 +3635,13 @@ static void valueFlowLifetimeFunction(Token *tok, TokenList *tokenlist, ErrorLog
if (i->container != returnContainer)
continue;
const Token * const argTok = args[argnr - 1];
LifetimeStore{argTok, "Passed to '" + tok->str() + "'.", ValueFlow::Value::LifetimeKind::Iterator} .byVal(
tok->next(), tokenlist, errorLogger, settings);
// Check if lifetime is available to avoid adding the lifetime twice
ValueFlow::Value val = getLifetimeObjValue(argTok);
if (val.tokvalue) {
LifetimeStore{argTok, "Passed to '" + tok->str() + "'.", ValueFlow::Value::LifetimeKind::Iterator} .byVal(
tok->next(), tokenlist, errorLogger, settings);
break;
}
}
} else if (Token::Match(tok->tokAt(-2), "std :: ref|cref|tie|front_inserter|back_inserter")) {
for (const Token *argtok : getArguments(tok)) {

View File

@ -65,6 +65,7 @@ private:
TEST_CASE(iterator20);
TEST_CASE(iterator21);
TEST_CASE(iterator22);
TEST_CASE(iterator23);
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
@ -1056,6 +1057,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void iterator23() { // #9550
check("struct A {\n"
" struct B {\n"
" bool operator==(const A::B& b) const;\n"
" int x;\n"
" int y;\n"
" int z;\n"
" };\n"
"};\n"
"bool A::B::operator==(const A::B& b) const {\n"
" return std::tie(x, y, z) == std::tie(b.x, b.y, b.z);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void iteratorExpression() {
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"