From 5342e2cd036ac8fd73f5edc9a0189a7276795375 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 20 Jul 2022 20:48:17 +0200 Subject: [PATCH] Fix #11037 FP knownConditionTrueFalse with dynamic_cast (#4297) --- lib/valueflow.cpp | 3 +-- test/testcondition.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 31a00bcbc..6283b5406 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index a270641da..fc8a9f16d 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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&p) {\n" + " const auto d1 = dynamic_cast(p.get());\n" + " const auto d2 = dynamic_cast(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"