From 510748134f9cf4217020acc95e4a72b481a27d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 4 Aug 2019 10:21:16 +0200 Subject: [PATCH] Fixed #9234 (FP unreadVariable - assignment via iterator) --- lib/checkunusedvar.cpp | 6 ++++++ test/testunusedvar.cpp | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index efb6b8793..553d84375 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -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 diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 274bda174..11249d389 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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"