From 71bc79ac28e83d4279f978c339350a81cb73b051 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 10 Sep 2020 22:58:17 -0500 Subject: [PATCH] Fix issue 9889: False positive: Using reference to dangling temporary with function object --- lib/astutils.cpp | 4 ++-- test/testautovariables.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 1572c7049..50485bc16 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -247,12 +247,12 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow return false; if (Token::Match(tok, "&|<<|>>") && isLikelyStream(cpp, tok->astOperand1())) return false; - if (Token::Match(tok->previous(), ">|%name% (")) { + if (Token::simpleMatch(tok, "(") && tok->astOperand1() && (tok->astOperand2() || Token::simpleMatch(tok->next(), ")"))) { if (tok->valueType()) { return tok->valueType()->reference == Reference::None; } const Token* ftok = nullptr; - if (tok->previous()->link()) + if (Token::simpleMatch(tok->previous(), ">") && tok->previous()->link()) ftok = tok->previous()->link()->previous(); else ftok = tok->previous(); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 2b5668ff0..6e4ba2c5e 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -1343,6 +1343,13 @@ private: " return a()();\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #9889 + check("int f(std::vector>& v, int i) {\n" + " auto& j = v[i]();\n" + " return j;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void returnReferenceFunction() {