Fix issue 9598: False positive: Using iterator to local container that may be invalid for loop handling (#2539)
This commit is contained in:
parent
a350ed9bc2
commit
95a48eac67
|
@ -749,8 +749,18 @@ void CheckStl::invalidContainer()
|
||||||
continue;
|
continue;
|
||||||
std::set<nonneg int> skipVarIds;
|
std::set<nonneg int> skipVarIds;
|
||||||
// Skip if the variable is assigned to
|
// Skip if the variable is assigned to
|
||||||
if (Token::Match(tok->astTop(), "%assign%") && Token::Match(tok->astTop()->previous(), "%var%"))
|
const Token* assignExpr = tok;
|
||||||
skipVarIds.insert(tok->astTop()->previous()->varId());
|
while (assignExpr->astParent()) {
|
||||||
|
bool isRHS = astIsRHS(assignExpr);
|
||||||
|
assignExpr = assignExpr->astParent();
|
||||||
|
if (Token::Match(assignExpr, "%assign%")) {
|
||||||
|
if (!isRHS)
|
||||||
|
assignExpr = nullptr;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Token::Match(assignExpr, "%assign%") && Token::Match(assignExpr->astOperand1(), "%var%"))
|
||||||
|
skipVarIds.insert(assignExpr->astOperand1()->varId());
|
||||||
const Token * endToken = nextAfterAstRightmostLeaf(tok->next()->astParent());
|
const Token * endToken = nextAfterAstRightmostLeaf(tok->next()->astParent());
|
||||||
if (!endToken)
|
if (!endToken)
|
||||||
endToken = tok->next();
|
endToken = tok->next();
|
||||||
|
|
|
@ -4111,6 +4111,13 @@ private:
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n",true);
|
"}\n",true);
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
// #9598
|
||||||
|
check("void f(std::vector<std::string> v) {\n"
|
||||||
|
" for (auto it = v.begin(); it != v.end(); it = v.erase(it))\n"
|
||||||
|
" *it;\n"
|
||||||
|
"}\n",true);
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void findInsert() {
|
void findInsert() {
|
||||||
|
|
Loading…
Reference in New Issue