Fix FP incorrectCharBooleanError with known condition (#5365)
This commit is contained in:
parent
4654b6561b
commit
394996581e
|
@ -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"))
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue