diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 0d9b6b78bca..0818086b0ec 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -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")) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 9db2ab90704..7073d4a9cff 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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" diff --git a/test/teststring.cpp b/test/teststring.cpp index 05e88abd135..e94f969034d 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -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() {