diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c5d9e31b7..b1e6d1846 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1065,7 +1065,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us if (!bFirstAssignment && Token::Match(tok, "* %varid%", var->declarationId())) // dereferencing means access to previous content return false; - if (Token::Match(tok, "= %varid%", var->declarationId()) && (var->isArray() || var->isPointer())) // Create a copy of array/pointer. Bailout, because the memory it points to might be necessary in outer scope + if (Token::Match(tok, "= %varid%", var->declarationId()) && (var->isArray() || var->isPointer() || (var->valueType() && var->valueType()->container))) // Create a copy of array/pointer. Bailout, because the memory it points to might be necessary in outer scope return false; if (tok->varId() == var->declarationId()) { diff --git a/test/testother.cpp b/test/testother.cpp index f0be96323..0d3512054 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1346,6 +1346,19 @@ private: " }\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 's' can be reduced.\n", errout.str()); + + check("auto foo(std::vector& vec, bool flag) {\n" + " std::vector dummy;\n" + " std::vector::iterator iter;\n" + " if (flag)\n" + " iter = vec.begin();\n" + " else {\n" + " dummy.push_back(42);\n" + " iter = dummy.begin();\n" + " }\n" + " return *iter;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void varScope30() { // #8541