Fix #11929 FP knownPointerToBool with dynamic_cast (#5416)

This commit is contained in:
chrchr-github 2023-09-11 18:04:36 +02:00 committed by GitHub
parent cbbf500cd4
commit c6721688e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -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() &&

View File

@ -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<D*>(b)) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};