From da32c5aecd44b1022978ae0fc59e54253670a330 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Mon, 19 Dec 2022 15:28:59 -0600 Subject: [PATCH] Fix 11442: new "Reference to temporary returned." FPs (#4657) --- lib/astutils.cpp | 2 ++ test/testautovariables.cpp | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 7264aecaf..0de86bb27 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -407,6 +407,8 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow const Token* branchTok = tok->astOperand2(); if (!branchTok->astOperand1()->valueType()) return false; + if (!branchTok->astOperand2()->valueType()) + return false; return !branchTok->astOperand1()->valueType()->isTypeEqual(branchTok->astOperand2()->valueType()); } if (Token::simpleMatch(tok, "(") && tok->astOperand1() && diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 2efdb20d0..3ddc8325b 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3733,6 +3733,14 @@ private: "}\n", true); ASSERT_EQUALS("", errout.str()); + + // #11442 + check("const std::string& f(const P< std::string >& value) {\n" + " static const std::string empty;\n" + " return value.get() == nullptr ? empty : *value;\n" + "}\n", + true); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeBorrowedMembers()