diff --git a/src/checkother.cpp b/src/checkother.cpp index 245c37add..cdd91b4f0 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -647,15 +647,31 @@ void CheckOther::CheckConstantFunctionParameter() void CheckOther::CheckStructMemberUsage() { const char *structname = 0; - for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (tok->fileIndex() != 0) continue; + + if (Token::Match(tok, "struct|union %type% {")) + { + structname = tok->strAt(1); + + // Bail out if struct/union contain any functions + for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) + { + if (tok2->str() == "(") + { + structname = 0; + break; + } + + if (tok2->str() == "}") + break; + } + } + if (tok->str() == "}") structname = 0; - if (Token::Match(tok, "struct|union %type% {")) - structname = tok->strAt(1); if (structname && Token::Match(tok, "[{;]")) { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 84d45c892..8a457fbdb 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -57,6 +57,7 @@ private: TEST_CASE(structmember2); TEST_CASE(structmember3); TEST_CASE(structmember4); + TEST_CASE(structmember5); TEST_CASE(localvar1); TEST_CASE(localvar2); @@ -144,6 +145,28 @@ private: } + void structmember5() + { + check("struct AB\n" + "{\n" + " int a;\n" + " int b;\n" + " void reset()\n" + " {\n" + " a = 1;\n" + " b = 2;\n" + " }\n" + "};\n" + "\n" + "void foo()\n" + "{\n" + " struct AB ab;\n" + " ab.reset();\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + } + +