From e319a92a5ced31d2ea82bc9eadc5434767288ba9 Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Wed, 11 Nov 2026 07:20:54 +0100 Subject: [PATCH] Fix #15204 FP unusedFunction with compound literal as first argument 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. --- lib/checkunusedfunctions.cpp | 4 ++-- test/testunusedfunctions.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 4177a13a013..d55a24f0273 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -275,8 +275,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Library if (Token::Match(funcname, "%name% (|<") && funcname->linkAt(1)) { const Token *ftok = funcname->next(); if (ftok->str() == "<") - ftok = ftok->link(); - if (Token::Match(ftok->linkAt(1), ") const|throw|{")) + ftok = ftok->link()->next(); + if (Token::simpleMatch(ftok, "(") && Token::Match(ftok->link(), ") const|throw|{")) funcname = nullptr; } diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index e38cfc25df0..b960dc57ff6 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -87,6 +87,7 @@ class TestUnusedFunctions : public TestFixture { TEST_CASE(attributeMaybeUnused); TEST_CASE(staticFunction); TEST_CASE(unnamedArg); + TEST_CASE(compoundLiteralArg); } struct CheckOptions @@ -838,6 +839,16 @@ class TestUnusedFunctions : public TestFixture { "[[unused]] void g(Type, Type) {}\n"); ASSERT_EQUALS("[test.cpp:2:10]: (style) The function 'Type' is never used. [unusedFunction]\n", errout_str()); } + + void compoundLiteralArg() + { + check("struct S { int i; };\n" + "static struct S f(struct S s) { return s; }\n" + "int main(void) {\n" + " return f((struct S){0}).i;\n" + "}\n", dinit(CheckOptions, $.cpp = false)); + ASSERT_EQUALS("", errout_str()); + } }; REGISTER_TEST(TestUnusedFunctions)