Skip to content

Commit e0596d3

Browse files
authored
Fix #15204 FP unusedFunction with compound literal as first argument (#9015)
The check meant to skip function declarations and definitions inspected the link of the token following the opening parenthesis, i.e. the first argument, rather than the parenthesis itself. A call such as `f((struct S){0})` was therefore mistaken for a definition and the call was never recorded.
1 parent fdc1942 commit e0596d3

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎lib/checkunusedfunctions.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Library
275275
if (Token::Match(funcname, "%name% (|<") && funcname->linkAt(1)) {
276276
const Token *ftok = funcname->next();
277277
if (ftok->str() == "<")
278-
ftok = ftok->link();
279-
if (Token::Match(ftok->linkAt(1), ") const|throw|{"))
278+
ftok = ftok->link()->next();
279+
if (Token::simpleMatch(ftok, "(") && Token::Match(ftok->link(), ") const|throw|{"))
280280
funcname = nullptr;
281281
}
282282

‎test/testunusedfunctions.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class TestUnusedFunctions : public TestFixture {
8787
TEST_CASE(attributeMaybeUnused);
8888
TEST_CASE(staticFunction);
8989
TEST_CASE(unnamedArg);
90+
TEST_CASE(compoundLiteralArg);
9091
}
9192

9293
struct CheckOptions
@@ -838,6 +839,16 @@ class TestUnusedFunctions : public TestFixture {
838839
"[[unused]] void g(Type, Type) {}\n");
839840
ASSERT_EQUALS("[test.cpp:2:10]: (style) The function 'Type' is never used. [unusedFunction]\n", errout_str());
840841
}
842+
843+
void compoundLiteralArg()
844+
{
845+
check("struct S { int i; };\n"
846+
"static struct S f(struct S s) { return s; }\n"
847+
"int main(void) {\n"
848+
" return f((struct S){0}).i;\n"
849+
"}\n", dinit(CheckOptions, $.cpp = false));
850+
ASSERT_EQUALS("", errout_str());
851+
}
841852
};
842853

843854
REGISTER_TEST(TestUnusedFunctions)

0 commit comments

Comments
 (0)