Fixed #5246 (false positive: (warning) Logical conjunction always evaluates to false: t > 0 && t < 1.)
This commit is contained in:
parent
8c40f4fee0
commit
486a3192c0
|
@ -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<std::string> &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)
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue