From 51095a0a073d7bcd72f15b923d079aba823a58b4 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 15 Jul 2025 10:37:50 +0200 Subject: [PATCH 1/4] Update teststl.cpp --- test/teststl.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/teststl.cpp b/test/teststl.cpp index b102f979ca3..f66ad45c555 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -6533,6 +6533,17 @@ class TestStl : public TestFixture { ASSERT_EQUALS( "[test.cpp:4:7] -> [test.cpp:7:5] -> [test.cpp:8:7]: (error) Calling 'add' while iterating the container is invalid. [invalidContainerLoop]\n", errout_str()); + + check("struct S { int i; };\n" // #14013 + "void f() {\n" + " std::vector> v;\n" + " for (int i = 0; i < 5; ++i) {\n" + " std::unique_ptr& r = v.emplace_back(std::make_unique());\n" + " r->i = 1;\n" + " }\n" + "}\n", + dinit(CheckOptions, $.inconclusive = true)); + ASSERT_EQUALS("", errout_str()); } void findInsert() { From c17b7e26d3eda6b00107acf8b0b0be27cc872e7c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 15 Jul 2025 10:38:42 +0200 Subject: [PATCH 2/4] Update checkstl.cpp --- lib/checkstl.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 8c2290fc098..3d8328da8fb 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1202,6 +1202,10 @@ void CheckStl::invalidContainer() if (info.tok->variable()->isReference() && !isVariableDecl(info.tok) && reaches(info.tok->variable()->nameToken(), tok, nullptr)) { + if (assignExpr && Token::Match(assignExpr->astOperand1(), "& %varid%", info.tok->varId())) { // TODO: fix AST + return false; + } + ErrorPath ep; bool addressOf = false; const Variable* var = ValueFlow::getLifetimeVariable(info.tok, ep, *mSettings, &addressOf); From 036aa9b1fc94be7cf1ec2a020e6828c7fd6a9258 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 15 Jul 2025 11:01:12 +0200 Subject: [PATCH 3/4] Update teststl.cpp --- test/teststl.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/teststl.cpp b/test/teststl.cpp index f66ad45c555..fb996965189 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -6544,6 +6544,17 @@ class TestStl : public TestFixture { "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); + + check("struct S { int i; };\n" + "void f() {\n" + " std::vector> v;\n" + " for (int i = 0; i < 5; ++i) {\n" + " std::unique_ptr& r{ v.emplace_back(std::make_unique()) };\n" + " r->i = 1;\n" + " }\n" + "}\n", + dinit(CheckOptions, $.inconclusive = true)); + ASSERT_EQUALS("", errout_str()); } void findInsert() { From 65dfadbe2ec84db06bf5d9d81877a05b4dcd1edd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 15 Jul 2025 11:02:44 +0200 Subject: [PATCH 4/4] Update checkstl.cpp --- lib/checkstl.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 3d8328da8fb..b19b08285b0 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1202,7 +1202,8 @@ void CheckStl::invalidContainer() if (info.tok->variable()->isReference() && !isVariableDecl(info.tok) && reaches(info.tok->variable()->nameToken(), tok, nullptr)) { - if (assignExpr && Token::Match(assignExpr->astOperand1(), "& %varid%", info.tok->varId())) { // TODO: fix AST + if ((assignExpr && Token::Match(assignExpr->astOperand1(), "& %varid%", info.tok->varId())) || // TODO: fix AST + Token::Match(assignExpr, "& %varid% {|(", info.tok->varId())) { return false; }