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"