Handle overloads and custom implementations of functions checkReturnIgnoredReturnValue() to avoid false positives

This commit is contained in:
PKEuS 2014-10-30 21:19:51 +01:00
parent f75aca1921
commit b7996bd0b0
2 changed files with 13 additions and 1 deletions

View File

@ -2757,7 +2757,7 @@ void CheckOther::checkReturnIgnoredReturnValue()
if (tok->varId() || !Token::Match(tok, "%var% (") || tok->strAt(-1) == ".")
continue;
if (!tok->next()->astParent() && _settings->library.useretval.find(tok->str()) != _settings->library.useretval.end())
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());
}
}

View File

@ -6265,6 +6265,18 @@ private:
"}", "test.cpp", false, false, false, true, &settings_std);
ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function strcmp() is not used.\n", errout.str());
check("bool strcmp(char* a, char* b);\n" // cppcheck sees a custom strcmp definition, but it returns a value. Assume it is the one specified in the library.
"void foo() {\n"
" strcmp(a, b);\n"
"}", "test.cpp", false, false, false, true, &settings_std);
ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function strcmp() is not used.\n", errout.str());
check("void strcmp(char* a, char* b);\n" // cppcheck sees a custom strcmp definition which returns void!
"void foo() {\n"
" strcmp(a, b);\n"
"}", "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);