fix #2827 to use numeric comparisons

This commit is contained in:
Robert Reif 2011-07-16 23:05:35 -04:00
parent 430d22032d
commit 4149617978
4 changed files with 107 additions and 14 deletions

View File

@ -745,14 +745,14 @@ void CheckOther::checkIncorrectLogicOperator()
{ "(", true, Second, "<", "&&", First, "<", ")", true, MoreEqual, false }, // (3 < x) && (x < 1) <- always false
{ "(", true, Second, ">", "&&", Second, "<", ")", true, LessEqual, false }, // (1 > x) && (3 < x) <- always false
{ "(", true, Second, "<", "&&", Second, ">", ")", true, MoreEqual, false }, // (3 < x) && (1 > x) <- always false
{ "(", true, First , ">", "||", First, "<", ")", true, More, true }, // (x > 3) || (x < 10) <- always true
{ "(", true, First , "<", "||", First, ">", ")", true, Less, true }, // (x < 10) || (x > 3) <- always true
{ "(", true, Second, "<", "||", First, "<", ")", true, More, true }, // (3 < x) || (x < 10) <- always true
{ "(", true, First, "<", "||", Second, "<", ")", true, Less, true }, // (x < 10) || (3 < x) <- always true
{ "(", true, First, ">", "||", Second, ">", ")", true, More, true }, // (x > 3) || (10 > x) <- always true
{ "(", true, Second, ">", "||", First, ">", ")", true, Less, true }, // (10 > x) || (x > 3) <- always true
{ "(", true, Second, "<", "||", Second, ">", ")", true, More, true }, // (3 < x) || (10 > x) <- always true
{ "(", true, Second, ">", "||", Second, "<", ")", true, Less, true }, // (10 > x) || (3 < x) <- always true
{ "(", true, First , ">", "||", First, "<", ")", true, Less, true }, // (x > 3) || (x < 10) <- always true
{ "(", true, First , "<", "||", First, ">", ")", true, More, true }, // (x < 10) || (x > 3) <- always true
{ "(", true, Second, "<", "||", First, "<", ")", true, Less, true }, // (3 < x) || (x < 10) <- always true
{ "(", true, First, "<", "||", Second, "<", ")", true, More, true }, // (x < 10) || (3 < x) <- always true
{ "(", true, First, ">", "||", Second, ">", ")", true, Less, true }, // (x > 3) || (10 > x) <- always true
{ "(", true, Second, ">", "||", First, ">", ")", true, More, true }, // (10 > x) || (x > 3) <- always true
{ "(", true, Second, "<", "||", Second, ">", ")", true, Less, true }, // (3 < x) || (10 > x) <- always true
{ "(", true, Second, ">", "||", Second, "<", ")", true, More, true }, // (10 > x) || (3 < x) <- always true
};
for (unsigned int i = 0; i < (sizeof(conditions) / sizeof(conditions[0])); i++)
@ -778,12 +778,12 @@ void CheckOther::checkIncorrectLogicOperator()
if (!((conditions[i].afterEqual && (conditions[i].after == nextTok->str())) || (!conditions[i].afterEqual && (conditions[i].after != nextTok->str()))))
continue;
if ((conditions[i].relation == Equal && firstConstant == secondConstant) ||
(conditions[i].relation == NotEqual && firstConstant != secondConstant) ||
(conditions[i].relation == Less && firstConstant < secondConstant) ||
(conditions[i].relation == LessEqual && firstConstant <= secondConstant) ||
(conditions[i].relation == More && firstConstant > secondConstant) ||
(conditions[i].relation == MoreEqual && firstConstant >= secondConstant))
if ((conditions[i].relation == Equal && MathLib::isEqual(firstConstant, secondConstant)) ||
(conditions[i].relation == NotEqual && MathLib::isNotEqual(firstConstant, secondConstant)) ||
(conditions[i].relation == Less && MathLib::isLess(firstConstant, secondConstant)) ||
(conditions[i].relation == LessEqual && MathLib::isLessEqual(firstConstant, secondConstant)) ||
(conditions[i].relation == More && MathLib::isGreater(firstConstant, secondConstant)) ||
(conditions[i].relation == MoreEqual && MathLib::isGreaterEqual(firstConstant, secondConstant)))
incorrectLogicOperatorError(term1Tok, conditions[i].state);
}
}

View File

@ -297,11 +297,36 @@ std::string MathLib::abs(const std::string &tok)
return toString<double>(std::abs(toDoubleNumber(tok)));
}
bool MathLib::isEqual(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) == toDoubleNumber(second);
}
bool MathLib::isNotEqual(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) != toDoubleNumber(second);
}
bool MathLib::isGreater(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) > toDoubleNumber(second);
}
bool MathLib::isGreaterEqual(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) >= toDoubleNumber(second);
}
bool MathLib::isLess(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) < toDoubleNumber(second);
}
bool MathLib::isLessEqual(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) <= toDoubleNumber(second);
}
bool MathLib::isNullValue(const std::string &str)
{
return (str == "-0" || str == "-0.0"

View File

@ -66,7 +66,12 @@ public:
static std::string cos(const std::string & tok);
static std::string tan(const std::string & tok);
static std::string abs(const std::string & tok);
static bool isEqual(const std::string & first, const std::string & second);
static bool isNotEqual(const std::string & first, const std::string & second);
static bool isGreater(const std::string & first, const std::string & second);
static bool isGreaterEqual(const std::string & first, const std::string & second);
static bool isLess(const std::string & first, const std::string & second);
static bool isLessEqual(const std::string & first, const std::string & second);
static bool isNullValue(const std::string &tok);
/**
* Return true if given character is 0,1,2,3,4,5,6 or 7.

View File

@ -2138,6 +2138,27 @@ private:
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(int x) {\n"
" if ((x == 1.0) && (x == 3.0))\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(float x) {\n"
" if ((x == 1) && (x == 1.0))\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((x == 1) && (x == 0x00000001))\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if (x == 1 && x == 3)\n"
" a++;\n"
@ -2145,6 +2166,41 @@ private:
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(int x) {\n"
" if (x == 1.0 && x == 3.0)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(float x) {\n"
" if (x == 1 && x == 1.0)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if (x < 1 && x > 1)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(int x) {\n"
" if (x < 1.0 && x > 1.0)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(int x) {\n"
" if (x < 1 && x > 1.0)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(int x) {\n"
" if (x < 1 && x > 3)\n"
" a++;\n"
@ -2152,6 +2208,13 @@ private:
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(float x) {\n"
" if (x < 1.0 && x > 3.0)\n"
" a++;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression always evaluates to false. Did you intend to use || instead?\n", errout.str());
check("void f(int x) {\n"
" if (1 > x && 3 < x)\n"
" a++;\n"