False positive dynamic_cast auto variable which has been checked against null (#2769)

This commit is contained in:
shaneasd 2020-09-04 13:06:26 +08:00 committed by GitHub
parent dba7108832
commit 84dd0c961f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

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

View File

@ -3306,6 +3306,17 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("int foo() {\n"
" auto x = getX();\n"
" if (x == nullptr)\n"
" return 1;\n"
" auto y = dynamic_cast<Y*>(x)\n"
" if (y == nullptr)\n"
" return 2;\n"
" return 3;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// handleKnownValuesInLoop
check("bool g();\n"
"void f(bool x) {\n"