Fix #12173 FP danglingTempReference with loop over init list (#5652)

This commit is contained in:
chrchr-github 2023-11-12 10:19:45 +01:00 committed by GitHub
parent f4df28d5ec
commit 212d2141d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -471,6 +471,8 @@ static bool isDeadTemporary(bool cpp, const Token* tok, const Token* expr, const
if (!precedes(nextAfterAstRightmostLeaf(tok->astTop()), nextAfterAstRightmostLeaf(expr->astTop())))
return false;
const Token* parent = tok->astParent();
if (Token::simpleMatch(parent, "{") && Token::simpleMatch(parent->astParent(), ":"))
parent = parent->astParent();
// Is in a for loop
if (astIsRHS(tok) && Token::simpleMatch(parent, ":") && Token::simpleMatch(parent->astParent(), "(") && Token::simpleMatch(parent->astParent()->previous(), "for (")) {
const Token* braces = parent->astParent()->link()->next();

View File

@ -2144,6 +2144,15 @@ private:
" (void)s.i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:9]: (error) Using reference to dangling temporary.\n", errout.str());
check("std::string f() {\n" // #12173
" std::string s;\n"
" for (auto& c : { \"a\", \"b\", \"c\" }) {\n"
" s += c;\n"
" }\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void testglobalnamespace() {