From c6721688e2fcecb0b36819efed89d7450de780cf Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 11 Sep 2023 18:04:36 +0200 Subject: [PATCH] Fix #11929 FP knownPointerToBool with dynamic_cast (#5416) --- lib/astutils.cpp | 2 +- test/testother.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 462e862b5..8eec9f04c 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1467,7 +1467,7 @@ bool isUsedAsBool(const Token* const tok, const Settings* settings) if (Token::Match(parent, "&&|!|%oror%")) return true; if (parent->isCast()) - return isUsedAsBool(parent); + return !Token::simpleMatch(parent->astOperand1(), "dynamic_cast") && isUsedAsBool(parent); if (parent->isUnaryOp("*")) return isUsedAsBool(parent); if (Token::Match(parent, "==|!=") && (tok->astSibling()->isNumber() || tok->astSibling()->isKeyword()) && tok->astSibling()->hasKnownIntValue() && diff --git a/test/testother.cpp b/test/testother.cpp index 6ad27e8e5..d6dfc0d63 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -11460,6 +11460,15 @@ private: " return A{x};\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Pointer expression 'x' converted to bool is always false.\n", errout.str()); + + check("struct B { virtual void f() {} };\n" // #11929 + "struct D : B {};\n" + "void g(B* b) {\n" + " if (!b)\n" + " return;\n" + " if (dynamic_cast(b)) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } };