From 796ad6c008d85b52bdb8b6633911c51a40150b8d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 25 Mar 2022 09:21:17 +0100 Subject: [PATCH] Fix #10913 FP variableScope - vector referenced via iterator (#3936) --- lib/checkother.cpp | 2 +- test/testother.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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