diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ffcca8394..93846ae7a 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3068,7 +3068,7 @@ static bool isLifetimeBorrowed(const ValueType *vt, const ValueType *vtParent) return false; if (!vt) return false; - if (vt->type != ValueType::UNKNOWN_TYPE && vtParent->type != ValueType::UNKNOWN_TYPE) { + if (vt->type != ValueType::UNKNOWN_TYPE && vtParent->type != ValueType::UNKNOWN_TYPE && vtParent->container == vt->container) { if (vtParent->pointer > vt->pointer) return true; if (vtParent->pointer < vt->pointer && vtParent->isIntegral()) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 18ababc8d..34bb9c0be 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2328,6 +2328,16 @@ private: " return value;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #9770 + check("class C {\n" + " std::string f(const char*);\n" + "};\n" + "std::string C::f(const char*) {\n" + " const char data[] = \"x\";\n" + " return data;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeFunction() {