diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index bc1870d0d85..276d49f7b30 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2205,6 +2205,8 @@ static Analyzer::Result valueFlowForward(Token* startToken, const Function* f = Scope::nestedInFunction(startToken->scope()); if (f && f->functionScope) endToken = f->functionScope->bodyEnd; + if (!endToken && exprTok && exprTok->variable() && !exprTok->variable()->isLocal()) + endToken = startToken->scope()->bodyEnd; return valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc); } diff --git a/test/teststl.cpp b/test/teststl.cpp index f3d7b941f82..07ca9d419f1 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -6738,6 +6738,21 @@ class TestStl : public TestFixture { "}\n", true); ASSERT_EQUALS("", errout_str()); + + check("struct S {\n" // #12757 + " template\n" + " void clear() {}\n" + " template\n" + " std::vector get() const { return {}; }\n" + " std::vector m;\n" + "};\n" + "template<> void S::clear() { m.clear(); }\n" + "template<> std::vector S::get() const {\n" + " for (const auto& i : m) {}\n" + " return {};\n" + "}\n", + true); + ASSERT_EQUALS("", errout_str()); } void checkMutexes() {