diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a698ecff4..14b53a6b2 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2908,7 +2908,7 @@ void CheckOther::checkMathFunctions() } // sqrt( x ): if x is negative the result is undefined else if (tok->varId() == 0 && - Token::Match(tok, "sqrt ( %num% )") && + Token::Match(tok, "sqrt|sqrtf|sqrtl ( %num% )") && MathLib::isNegative(tok->tokAt(2)->str())) { mathfunctionCallError(tok); @@ -3257,6 +3257,17 @@ void CheckOther::checkDuplicateExpression() if (Token::Match(tok, "(|&&|%oror% %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% )|&&|%oror%") && tok->strAt(1) == tok->strAt(3)) { + // float == float and float != float are valid NaN checks + if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId()) + { + const Variable * var = symbolDatabase->getVariableFromVarId(tok->next()->varId()); + if (var && var->typeStartToken() == var->typeEndToken()) + { + if (Token::Match(var->typeStartToken(), "float|double")) + continue; + } + } + duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2)); } else if (Token::Match(tok, "(|&&|%oror% %var% . %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% . %var% )|&&|%oror%") && diff --git a/test/testother.cpp b/test/testother.cpp index 2fd0006c1..9466e0c8e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -115,7 +115,8 @@ private: TEST_CASE(duplicateIf); TEST_CASE(duplicateBranch); - TEST_CASE(duplicateExpression); + TEST_CASE(duplicateExpression1); + TEST_CASE(duplicateExpression2); // ticket #2730 } void check(const char code[], const char *filename = NULL) @@ -2502,7 +2503,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void duplicateExpression() + void duplicateExpression1() { check("voif foo() {\n" " if (a == a) { }\n" @@ -2523,6 +2524,27 @@ private: "[test.cpp:5] -> [test.cpp:5]: (style) Same expression on both sides of '>'.\n" "[test.cpp:6] -> [test.cpp:6]: (style) Same expression on both sides of '<'.\n", errout.str()); } + + void duplicateExpression2() // ticket #2730 + { + check("int main()\n" + "{\n" + " long double ldbl;\n" + " double dbl, in;\n" + " float flt;\n" + " int have_nan = 0;\n" + " ldbl = sqrtl(-1.0);\n" + " dbl = sqrt(-1.0);\n" + " flt = sqrtf(-1.0);\n" + " if (ldbl != ldbl) have_nan = 1;\n" + " if (!(dbl == dbl)) have_nan = 1;\n" + " if (flt != flt) have_nan = 1;\n" + " return have_nan;\n" + "}"); + ASSERT_EQUALS("[test.cpp:7]: (error) Passing value -1.0 to sqrtl() leads to undefined result\n" + "[test.cpp:8]: (error) Passing value -1.0 to sqrt() leads to undefined result\n" + "[test.cpp:9]: (error) Passing value -1.0 to sqrtf() leads to undefined result\n", errout.str()); + } }; REGISTER_TEST(TestOther)