From 2bccde0e3b4becd17e3a52e88c6fce750697e219 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 9 Apr 2022 14:09:10 +0200 Subject: [PATCH] Fix #9789 FP Logical condition considered always false (#3985) --- lib/checkcondition.cpp | 11 +++++++++++ test/testcondition.cpp | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index c86799fbe..9a3dd7483 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1134,6 +1134,11 @@ void CheckCondition::checkIncorrectLogicOperator() if (inconclusive && !printInconclusive) continue; + const bool isUnknown = (expr1 && expr1->valueType() && expr1->valueType()->type == ValueType::UNKNOWN_TYPE) || + (expr2 && expr2->valueType() && expr2->valueType()->type == ValueType::UNKNOWN_TYPE); + if (isUnknown) + continue; + const bool isfloat = astIsFloat(expr1, true) || MathLib::isFloat(value1) || astIsFloat(expr2, true) || MathLib::isFloat(value2); ErrorPath errorPath; @@ -1442,6 +1447,12 @@ void CheckCondition::alwaysTrueFalse() if (!(constIfWhileExpression || constValExpr || compExpr || ternaryExpression || returnExpression)) continue; + const Token* expr1 = tok->astOperand1(), *expr2 = tok->astOperand2(); + const bool isUnknown = (expr1 && expr1->valueType() && expr1->valueType()->type == ValueType::UNKNOWN_TYPE) || + (expr2 && expr2->valueType() && expr2->valueType()->type == ValueType::UNKNOWN_TYPE); + if (isUnknown) + continue; + // Don't warn when there are expanded macros.. bool isExpandedMacro = false; visitAstNodes(tok, [&](const Token * tok2) { diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 744b4da50..4c5c28fad 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1209,6 +1209,14 @@ private: " if (x && x != ZERO) {}\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f(int N) {\n" // #9789 + " T a[20] = { 0 };\n" + " for (int i = 0; i < N; ++i) {\n" + " if (0 < a[i] && a[i] < 1) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void incorrectLogicOperator5() { // complex expressions