@@ -725,14 +725,36 @@ static bool isSameIteratorContainerExpression(const Token* tok1,
725725 return false ;
726726}
727727
728+ // First it groups the lifetimes together using std::partition with the lifetimes that refer to the same token or token of a subexpression.
729+ // Then it finds the lifetime in that group that refers to the "highest" parent using std::min_element and adds that to the vector.
730+ static std::vector<ValueFlow::Value> pruneLifetimes (std::vector<ValueFlow::Value> lifetimes)
731+ {
732+ std::vector<ValueFlow::Value> result;
733+ auto start = lifetimes.begin ();
734+ while (start != lifetimes.end ())
735+ {
736+ const Token* tok1 = start->tokvalue ;
737+ auto it = std::partition (start, lifetimes.end (), [&](const ValueFlow::Value& v) {
738+ const Token* tok2 = v.tokvalue ;
739+ return astHasToken (tok1, tok2) || astHasToken (tok2, tok1);
740+ });
741+ auto root = std::min_element (start, it, [](const ValueFlow::Value& x, const ValueFlow::Value& y) {
742+ return x.tokvalue != y.tokvalue && astHasToken (x.tokvalue , y.tokvalue );
743+ });
744+ result.push_back (*root);
745+ start = it;
746+ }
747+ return result;
748+ }
749+
728750static ValueFlow::Value getLifetimeIteratorValue (const Token* tok, MathLib::bigint path = 0 )
729751{
730752 auto findIterVal = [](const std::vector<ValueFlow::Value>& values, const std::vector<ValueFlow::Value>::const_iterator beg) {
731753 return std::find_if (beg, values.cend (), [](const ValueFlow::Value& v) {
732754 return v.lifetimeKind == ValueFlow::Value::LifetimeKind::Iterator;
733755 });
734756 };
735- std::vector<ValueFlow::Value> values = ValueFlow::getLifetimeObjValues (tok, false , path);
757+ std::vector<ValueFlow::Value> values = pruneLifetimes ( ValueFlow::getLifetimeObjValues (tok, false , path) );
736758 auto it = findIterVal (values, values.begin ());
737759 if (it != values.end ()) {
738760 auto it2 = findIterVal (values, it + 1 );
0 commit comments