From b5ea67718059a792cb302d0a1c13391a61651adc Mon Sep 17 00:00:00 2001 From: PKEuS Date: Thu, 30 Oct 2014 23:24:13 +0100 Subject: [PATCH] Some fixes for CheckOther::checkIgnoredReturnValue(): - Fixed name of the function - Fixed false positive for code like "class strcmp { strcmp() {} };" - Fixed FP #6233 on checking side (no AST generated for function call) --- lib/checkother.cpp | 7 +++++-- lib/checkother.h | 4 ++-- test/testother.cpp | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a8db352e9..caf5b2300 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2744,7 +2744,7 @@ void CheckOther::varFuncNullUBError(const Token *tok) //--------------------------------------------------------------------------- // Check for ignored return values. //--------------------------------------------------------------------------- -void CheckOther::checkReturnIgnoredReturnValue() +void CheckOther::checkIgnoredReturnValue() { if (!_settings->isEnabled("warning")) return; @@ -2754,9 +2754,12 @@ void CheckOther::checkReturnIgnoredReturnValue() for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (tok->varId() || !Token::Match(tok, "%var% (") || tok->strAt(-1) == ".") + if (tok->varId() || !Token::Match(tok, "%var% (") || tok->strAt(-1) == "." || tok->next()->astOperand1() != tok) continue; + if (!tok->scope()->isExecutable()) + tok = tok->scope()->classEnd; + if (!tok->next()->astParent() && (!tok->function() || !Token::Match(tok->function()->retDef, "void %var%")) && _settings->library.useretval.find(tok->str()) != _settings->library.useretval.end()) ignoredReturnValueError(tok, tok->str()); } diff --git a/lib/checkother.h b/lib/checkother.h index 4bf256843..c4efb5140 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -73,7 +73,7 @@ public: checkOther.checkVarFuncNullUB(); checkOther.checkNanInArithmeticExpression(); checkOther.checkCommaSeparatedReturn(); - checkOther.checkReturnIgnoredReturnValue(); + checkOther.checkIgnoredReturnValue(); } /** @brief Run checks against the simplified token list */ @@ -231,7 +231,7 @@ public: void checkComparisonFunctionIsAlwaysTrueOrFalse(); /** @brief %Check for ignored return values. */ - void checkReturnIgnoredReturnValue(); + void checkIgnoredReturnValue(); private: // Error messages.. diff --git a/test/testother.cpp b/test/testother.cpp index dfd396959..c0a6b6326 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6277,6 +6277,11 @@ private: "}", "test.cpp", false, false, false, true, &settings_std); ASSERT_EQUALS("", errout.str()); + check("void foo() {\n" + " class strcmp { strcmp() {} };\n" // strcmp is a constructor definition here + "}", "test.cpp", false, false, false, true, &settings_std); + ASSERT_EQUALS("", errout.str()); + check("void foo() {\n" " return strcmp(a, b);\n" "}", "test.cpp", false, false, false, true, &settings_std); @@ -6303,6 +6308,17 @@ private: " DebugLog::getInstance().log(systemInfo.getSystemInfo());\n" "}", "test.cpp", false, false, false, true, &settings_std); ASSERT_EQUALS("", errout.str()); + + // #6233 + check("int main() {\n" + " auto lambda = [](double value) {\n" + " double rounded = floor(value + 0.5);\n" + " printf(\"Rounded value = %f\\n\", rounded);\n" + " };\n" + " lambda(13.3);\n" + " return 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } };