Skip to content

Commit

Permalink
Fix FP incorrectCharBooleanError with known condition
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 23, 2023
1 parent 22caa89 commit 53f9d96
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()->isLiteral()) && tok->astSibling()->hasKnownIntValue() &&
tok->astSibling()->values().front().intvalue == 0)
return true;
if (parent->str() == "(" && astIsRHS(tok) && Token::Match(parent->astOperand1(), "if|while"))
Expand Down
7 changes: 7 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3550,6 +3550,13 @@ class TestCondition : public TestFixture {
"}");
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!=0', second condition is always false\n", errout.str());

check("void f(void* x, void* y) {\n"
" if (x == nullptr && y == nullptr)\n"
" return;\n"
Expand Down
7 changes: 7 additions & 0 deletions test/teststring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,13 @@ class TestString : public TestFixture {
" 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() {
Expand Down

0 comments on commit 53f9d96

Please sign in to comment.