diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 4db081998..db30455a1 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -749,8 +749,18 @@ void CheckStl::invalidContainer() continue; std::set skipVarIds; // Skip if the variable is assigned to - if (Token::Match(tok->astTop(), "%assign%") && Token::Match(tok->astTop()->previous(), "%var%")) - skipVarIds.insert(tok->astTop()->previous()->varId()); + const Token* assignExpr = tok; + 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()); if (!endToken) endToken = tok->next(); diff --git a/test/teststl.cpp b/test/teststl.cpp index f6c83cd3b..3ca8cfab1 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4111,6 +4111,13 @@ private: " }\n" "}\n",true); ASSERT_EQUALS("", errout.str()); + + // #9598 + check("void f(std::vector 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() {