Fix FP incorrectCharBooleanError with known condition (#5365)

This commit is contained in:
chrchr-github 2023-08-24 10:36:01 +02:00 committed by GitHub
parent 4654b6561b
commit 394996581e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View File

@ -1455,7 +1455,7 @@ bool isUsedAsBool(const Token* const tok, const Settings* settings)
return isUsedAsBool(parent);
if (parent->isUnaryOp("*"))
return isUsedAsBool(parent);
if (Token::Match(parent, "==|!=") && tok->astSibling()->hasKnownIntValue() &&
if (Token::Match(parent, "==|!=") && (tok->astSibling()->isNumber() || tok->astSibling()->isKeyword()) && tok->astSibling()->hasKnownIntValue() &&
tok->astSibling()->values().front().intvalue == 0)
return true;
if (parent->str() == "(" && astIsRHS(tok) && Token::Match(parent->astOperand1(), "if|while"))

View File

@ -3550,6 +3550,13 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Identical condition 'handle!=0', second condition is always false\n", errout.str());
check("int f(void *handle) {\n"
" if (handle != nullptr) return 0;\n"
" if (handle) return 1;\n"
" else return 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Identical condition 'handle!=nullptr', second condition is always false\n", errout.str());
check("void f(void* x, void* y) {\n"
" if (x == nullptr && y == nullptr)\n"
" return;\n"

View File

@ -792,6 +792,13 @@ private:
" g(2, !\"def\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Conversion of string literal \"def\" to bool always evaluates to true.\n", errout.str());
check("bool f(const char *p) {\n"
" if (*p == '\\0')\n"
" return *p == '\\0';\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void deadStrcmp() {