From c9ed9a9639d00bc9f636196656272e93e380e02a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 7 Aug 2025 10:35:36 +0200 Subject: [PATCH 1/8] Update testtokenize.cpp --- test/testtokenize.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b6abf31b424..af34625ca18 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6788,6 +6788,7 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS("unoRef:: var0(", testAst(code1)); ASSERT_EQUALS("vary=", testAst("std::string var = y;")); + ASSERT_EQUALS("vary=", testAst("std::unique_ptr var = y;")); // #14019 ASSERT_EQUALS("", testAst("void *(*var)(int);")); ASSERT_EQUALS("", testAst("void *(*var[2])(int);")); From ca62d2da809c80249bfd8d2ba5935b68a2455277 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 7 Aug 2025 10:39:46 +0200 Subject: [PATCH 2/8] Update tokenlist.cpp --- lib/tokenlist.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 678abeafff5..cb655d18a04 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1765,9 +1765,18 @@ static Token * createAstAtToken(Token *tok) if (Token::Match(tok, "%type% %name%|*|&|&&|::") && !Token::Match(tok, "return|new|delete")) { int typecount = 0; Token *typetok = tok; - while (Token::Match(typetok, "%type%|::|*|&|&&")) { + while (Token::Match(typetok, "%type%|::|*|&|&&|<")) { if (typetok->isName() && !Token::simpleMatch(typetok->previous(), "::")) typecount++; + if (typetok->str() == "<") { + if (Token* closing = typetok->findClosingBracket()) { + typetok = closing->next(); + if (Token::simpleMatch(typetok, "::")) + typetok = typetok->next(); + continue; + } + break; + } typetok = typetok->next(); } if (Token::Match(typetok, "%var% =") && typetok->varId()) From 4754a7e8d315cdc97d61b12e83e28d3436bed83e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:15:08 +0200 Subject: [PATCH 3/8] Update valueflow.cpp --- lib/valueflow.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index a7198e33007..c4a438216fd 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1948,7 +1948,10 @@ static bool isLifetimeBorrowed(const ValueType *vt, const ValueType *vtParent) return true; if (vtParent->pointer < vt->pointer && vtParent->isIntegral()) return true; - if (vtParent->str() == vt->str()) + std::string vtParentStr = vtParent->str(); + if (startsWith(vtParentStr, "const ") // HACK: assignment to const + vtParentStr.erase(0, 6); + if (vtParentStr == vt->str()) return true; } From 1a5879d3da8079e331fec3763a9914faacef9d0d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:18:22 +0200 Subject: [PATCH 4/8] Update testautovariables.cpp --- test/testautovariables.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 453fadfaf50..8fb4525052f 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -4120,6 +4120,19 @@ class TestAutoVariables : public TestFixture { " (void)m->first;\n" "}\n"); ASSERT_EQUALS("[test.cpp:9:63] -> [test.cpp:9:49] -> [test.cpp:10:11]: (error) Using iterator that is a temporary. [danglingTemporaryLifetime]\n", + + check("struct A {\n" // #14054 + " std::map m_;\n" + "};\n" + "struct B {\n" + " A a_;\n" + "};\n" + "B func();\n" + "void f() {\n" + " const std::map::iterator m = func().a_.m_.begin();\n" + " (void)m->first;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:9:62] -> [test.cpp:9:48] -> [test.cpp:10:11]: (error) Using iterator that is a temporary. [danglingTemporaryLifetime]\n", errout_str()); check("void f(bool b) {\n" From 1a62386c7718601941256c69c25c6d1899a9ace0 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:21:40 +0200 Subject: [PATCH 5/8] Update valueflow.cpp --- lib/valueflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c4a438216fd..f249b829784 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1949,7 +1949,7 @@ static bool isLifetimeBorrowed(const ValueType *vt, const ValueType *vtParent) if (vtParent->pointer < vt->pointer && vtParent->isIntegral()) return true; std::string vtParentStr = vtParent->str(); - if (startsWith(vtParentStr, "const ") // HACK: assignment to const + if (startsWith(vtParentStr, "const ")) // HACK: assignment to const vtParentStr.erase(0, 6); if (vtParentStr == vt->str()) return true; From 8810c2bff7a8eeb2406d28d879697b697eefd966 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:26:06 +0200 Subject: [PATCH 6/8] Update testautovariables.cpp --- test/testautovariables.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 8fb4525052f..57fc43f80ce 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -4120,6 +4120,7 @@ class TestAutoVariables : public TestFixture { " (void)m->first;\n" "}\n"); ASSERT_EQUALS("[test.cpp:9:63] -> [test.cpp:9:49] -> [test.cpp:10:11]: (error) Using iterator that is a temporary. [danglingTemporaryLifetime]\n", + errout_str()); check("struct A {\n" // #14054 " std::map m_;\n" From 63628bb0d1d3ebf32379c65fc79f4a1a666f81b9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 8 Aug 2025 09:34:06 +0200 Subject: [PATCH 7/8] Update tokenlist.cpp --- lib/tokenlist.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index cb655d18a04..af49c65391a 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1771,8 +1771,6 @@ static Token * createAstAtToken(Token *tok) if (typetok->str() == "<") { if (Token* closing = typetok->findClosingBracket()) { typetok = closing->next(); - if (Token::simpleMatch(typetok, "::")) - typetok = typetok->next(); continue; } break; From cdfe9aef47af43cd5a4c11fd47cbe59004943ea0 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 23 Aug 2025 13:47:00 +0200 Subject: [PATCH 8/8] Improve c/v handling --- lib/valueflow.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f249b829784..e2d2f12f465 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1948,10 +1948,12 @@ static bool isLifetimeBorrowed(const ValueType *vt, const ValueType *vtParent) return true; if (vtParent->pointer < vt->pointer && vtParent->isIntegral()) return true; - std::string vtParentStr = vtParent->str(); - if (startsWith(vtParentStr, "const ")) // HACK: assignment to const - vtParentStr.erase(0, 6); - if (vtParentStr == vt->str()) + ValueType temp = *vtParent; + if ((temp.constness & 1) && !(vt->constness & 1)) // allow assignment to const/volatile + temp.constness &= ~1; + if ((temp.volatileness & 1) && !(vt->volatileness & 1)) + temp.volatileness &= ~1; + if (temp.str() == vt->str()) return true; }