Fix #11037 FP knownConditionTrueFalse with dynamic_cast (#4297)

This commit is contained in:
chrchr-github 2022-07-20 20:48:17 +02:00 committed by GitHub
parent 4316884123
commit 5342e2cd03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -800,8 +800,7 @@ static void setTokenValue(Token* tok,
// cast..
if (const Token *castType = getCastTypeStartToken(parent)) {
if (((tok->valueType() == nullptr && value.isImpossible()) || astIsPointer(tok)) &&
contains({ValueFlow::Value::ValueType::INT, ValueFlow::Value::ValueType::SYMBOLIC}, value.valueType) &&
if (contains({ValueFlow::Value::ValueType::INT, ValueFlow::Value::ValueType::SYMBOLIC}, value.valueType) &&
Token::simpleMatch(parent->astOperand1(), "dynamic_cast"))
return;
const ValueType &valueType = ValueType::parseDecl(castType, settings);

View File

@ -667,6 +667,17 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("class B { virtual void v() {} };\n" // #11037
"class D1 : public B {};\n"
"class D2 : public B {};\n"
"void f(const std::shared_ptr<B>&p) {\n"
" const auto d1 = dynamic_cast<D1*>(p.get());\n"
" const auto d2 = dynamic_cast<D2*>(p.get());\n"
" if (d1) {}\n"
" else if (d2) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n" // #6482
" if (x & 1) {}\n"
" else if (x == 0) {}\n"