From a858a0b6352c8e9c787ad5932c4dee4f327bb2c9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 25 May 2024 11:03:33 +0200 Subject: [PATCH] Fix #12757 FP knownEmptyContainer with template specialization (regression) (#6434) --- lib/valueflow.cpp | 2 ++ test/teststl.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) 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() {