Fix #9805 (False positive; Unused function (template)) (#2871)

* Add test
* Ignore templates with trailing return type as well
This commit is contained in:
vilarion 2020-11-01 15:10:02 +01:00 committed by GitHub
parent bd4dc364a7
commit 2c34e660b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -65,13 +65,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
continue;
// Don't care about templates
if (tokenizer.isCPP()) {
const Token *retDef = func->retDef;
while (retDef && retDef->isName())
retDef = retDef->previous();
if (retDef && retDef->str() == ">")
continue;
}
if (tokenizer.isCPP() && func->templateDef != nullptr)
continue;
mFunctionDecl.emplace_back(func);

View File

@ -46,6 +46,7 @@ private:
TEST_CASE(template1);
TEST_CASE(template2);
TEST_CASE(template3);
TEST_CASE(template4); // #9805
TEST_CASE(throwIsNotAFunction);
TEST_CASE(unusedError);
TEST_CASE(unusedMain);
@ -229,6 +230,26 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (style) The function 'bar' is never used.\n", errout.str());
}
void template4() { // #9805
check("struct A {\n"
" int a = 0;\n"
" void f() { a = 1; }\n"
" template <typename T, typename... Args> auto call(const Args &... args) -> T {\n"
" a = 2;\n"
" return T{};\n"
" }\n"
"};\n"
"\n"
"struct B : public A {\n"
" void test() {\n"
" f();\n"
" call<int>(1, 2, 3);\n"
" test();\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void throwIsNotAFunction() {
check("struct A {void f() const throw () {}}; int main() {A a; a.f();}");
ASSERT_EQUALS("", errout.str());