From 486a3192c03b1d73aba1048179d24b5c1bc7576d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 4 Feb 2014 06:49:49 +0100 Subject: [PATCH] Fixed #5246 (false positive: (warning) Logical conjunction always evaluates to false: t > 0 && t < 1.) --- lib/checkother.cpp | 24 ++++++++++++------------ test/testother.cpp | 5 +++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 75b31b73a..1c651e0ea 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -33,6 +33,17 @@ namespace { CheckOther instance; } +static bool astIsFloat(const Token *tok) +{ + if (tok->astOperand1() && astIsFloat(tok->astOperand1())) + return true; + if (tok->astOperand2() && astIsFloat(tok->astOperand2())) + return true; + + // TODO: check function calls, struct members, arrays, etc also + return !tok->variable() || Token::findmatch(tok->variable()->typeStartToken(), "float|double", tok->variable()->typeEndToken()->next(), 0); +} + static bool isConstExpression(const Token *tok, const std::set &constFunctions) { if (!tok) @@ -1362,7 +1373,7 @@ void CheckOther::checkIncorrectLogicOperator() if (!isSameExpression(expr1, expr2, constStandardFunctions)) continue; - const bool isfloat = MathLib::isFloat(value1) || MathLib::isFloat(value2); + const bool isfloat = astIsFloat(expr1) || MathLib::isFloat(value1) || astIsFloat(expr2) || MathLib::isFloat(value2); // don't check floating point equality comparisons. that is bad // and deserves different warnings. @@ -2770,17 +2781,6 @@ namespace { } } -static bool astIsFloat(const Token *tok) -{ - if (tok->astOperand1() && astIsFloat(tok->astOperand1())) - return true; - if (tok->astOperand2() && astIsFloat(tok->astOperand2())) - return true; - - // TODO: check function calls, struct members, arrays, etc also - return !tok->variable() || Token::findmatch(tok->variable()->typeStartToken(), "float|double", tok->variable()->typeEndToken()->next(), 0); -} - //--------------------------------------------------------------------------- // check for the same expression on both sides of an operator // (x == x), (x && x), (x || x) diff --git a/test/testother.cpp b/test/testother.cpp index 30e4851d0..3edbb6456 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3676,6 +3676,11 @@ private: ); ASSERT_EQUALS("", errout.str()); + check("void bar(float f) {\n" // #5246 + " if ((f > 0) && (f < 1)) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void f(int x) {\n" " if (x < 1 && x > 1)\n" " a++;\n"