Fix #10918 FP constStatement with dynamic_cast (#3954)

This commit is contained in:
chrchr-github 2022-03-28 22:15:40 +02:00 committed by GitHub
parent 8d49fc252c
commit 49147f95fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -1777,8 +1777,11 @@ static bool isConstStatement(const Token *tok, bool cpp)
return true;
if (Token::simpleMatch(tok->previous(), "sizeof ("))
return true;
if (isCPPCast(tok))
if (isCPPCast(tok)) {
if (Token::simpleMatch(tok->astOperand1(), "dynamic_cast") && Token::simpleMatch(tok->astOperand1()->next()->link()->previous(), "& >"))
return false;
return isWithoutSideEffects(cpp, tok) && isConstStatement(tok->astOperand2(), cpp);
}
else if (tok->isCast() && tok->next() && tok->next()->isStandardType())
return isWithoutSideEffects(cpp, tok->astOperand1()) && isConstStatement(tok->astOperand1(), cpp);
if (Token::simpleMatch(tok, "."))

View File

@ -283,6 +283,17 @@ private:
" ((struct foo *)(0x1234))->xy = 1;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("bool f(const std::exception& e) {\n" // #10918
" try {\n"
" dynamic_cast<const InvalidTypeException&>(e);\n"
" return true;\n"
" }\n"
" catch (...) {\n"
" return false;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void increment() {