From 06c83638524cfb63bd8f00a7c20b97ceb2b4046a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:12:40 +0100 Subject: [PATCH] Fix #11469 FP mismatchingContainerExpression warning (#5912) --- lib/astutils.cpp | 10 +++++++++- test/teststl.cpp | 7 ++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 2773e7e59a8..fa9ffc00edf 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -439,7 +439,15 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow if (Token::simpleMatch(tok->astOperand1(), "typeid")) return false; if (tok->valueType()) { - return tok->valueType()->reference == Reference::None; + if (tok->valueType()->pointer > 0) { + const Token* const parent = tok->astParent(); + if (Token::simpleMatch(parent, "&")) + return true; + if (Token::simpleMatch(parent, "return") && parent->valueType()->reference != Reference::None && + parent->valueType()->container && parent->valueType()->container->stdStringLike) + return true; + } + return tok->valueType()->reference == Reference::None && tok->valueType()->pointer == 0; } const Token* ftok = nullptr; if (Token::simpleMatch(tok->previous(), ">") && tok->previous()->link()) diff --git a/test/teststl.cpp b/test/teststl.cpp index 4426f3460a7..4369d6915e4 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -2031,17 +2031,14 @@ class TestStl : public TestFixture { "}\n"); ASSERT_EQUALS("", errout.str()); - check("struct S {\n" + check("struct S {\n" // #11469 " const std::vector* vec() const { return &v; }\n" " const std::vector v;\n" "};\n" "void f(const S& a, const S& b) {\n" " if (a.vec()->begin() - a.vec()->end() != b.vec()->begin() - b.vec()->end()) {}\n" "}\n"); - TODO_ASSERT_EQUALS("", - "[test.cpp:6]: (warning) Iterators to containers from different expressions 'a.vec()' and 'a.vec()' are used together.\n" - "[test.cpp:6]: (warning) Iterators to containers from different expressions 'b.vec()' and 'b.vec()' are used together.\n", - errout.str()); + ASSERT_EQUALS("", errout.str()); } void iteratorSameExpression() {