From 394996581e270d884124d60036f020e95d92ca17 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 24 Aug 2023 10:36:01 +0200 Subject: [PATCH] Fix FP incorrectCharBooleanError with known condition (#5365) --- lib/astutils.cpp | 2 +- test/testcondition.cpp | 7 +++++++ test/teststring.cpp | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 62204a9a0..53de61dcc 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()->isKeyword()) && 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 9db2ab907..f903dc7a4 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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" diff --git a/test/teststring.cpp b/test/teststring.cpp index 05e88abd1..e94f96903 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -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() {