Fixed #9234 (FP unreadVariable - assignment via iterator)

This commit is contained in:
Daniel Marjamäki 2019-08-04 10:21:16 +02:00
parent ce53931d00
commit 510748134f
2 changed files with 23 additions and 0 deletions

View File

@ -1174,6 +1174,12 @@ void CheckUnusedVar::checkFunctionVariableUsage()
if (!tok->astOperand1())
continue;
const Token *iteratorToken = tok->astOperand1();
while (Token::Match(iteratorToken, "[.*]"))
iteratorToken = iteratorToken->astOperand1();
if (iteratorToken && iteratorToken->variable() && iteratorToken->variable()->typeEndToken()->str().find("iterator") != std::string::npos)
continue;
const Variable *op1Var = tok->astOperand1() ? tok->astOperand1()->variable() : nullptr;
if (op1Var && op1Var->isReference() && op1Var->nameToken() != tok->astOperand1())
// todo: check references

View File

@ -129,6 +129,7 @@ private:
TEST_CASE(localvaralias15); // ticket #6315
TEST_CASE(localvaralias16);
TEST_CASE(localvaralias17); // ticket #8911
TEST_CASE(localvaralias18); // ticket #9234 - iterator
TEST_CASE(localvarasm);
TEST_CASE(localvarstatic);
TEST_CASE(localvarextern);
@ -3217,6 +3218,22 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvaralias18() {
functionVariableUsage("void add( std::vector< std::pair< int, double > >& v)\n"
"{\n"
" std::vector< std::pair< int, double > >::iterator it;\n"
" for ( it = v.begin(); it != v.end(); ++it )\n"
" {\n"
" if ( x )\n"
" {\n"
" ( *it ).second = 0;\n"
" break;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void localvarasm() {
functionVariableUsage("void foo(int &b)\n"