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()); } };