From b3c80947edab71340c2cc7d63fe0193bcd1ac6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 12 Nov 2017 22:33:17 +0100 Subject: [PATCH] Fixed #6508 (False positive unusedStructMember - brace initializer) --- lib/checkunusedvar.cpp | 5 +++++ test/testunusedvar.cpp | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 09e1f0bca..6e079c9b7 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1364,6 +1364,11 @@ void CheckUnusedVar::checkStructMemberUsage() if (Token::findmatch(scope->classEnd, castPattern.c_str())) continue; + // (struct S){..} + const std::string initPattern("( struct| " + scope->className + " ) {"); + if (Token::findmatch(scope->classEnd, initPattern.c_str())) + continue; + // Bail out if struct is used in sizeof.. for (const Token *tok = scope->classEnd; nullptr != (tok = Token::findsimplematch(tok, "sizeof ("));) { tok = tok->tokAt(2); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 3b21ed318..bca428583 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -51,6 +51,7 @@ private: TEST_CASE(structmember11); // #4168 - initialization with {} / passed by address to unknown function TEST_CASE(structmember12); // #7179 - FP unused structmember TEST_CASE(structmember13); // #3088 - __attribute__((packed)) + TEST_CASE(structmember14); // #6508 - (struct x){1,2,..} TEST_CASE(structmember_sizeof); TEST_CASE(localvar1); @@ -439,6 +440,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void structmember14() { // #6508 + checkStructMemberUsage("struct bstr { char *bstart; size_t len; };\n" + "struct bstr bstr0(void) {\n" + " return (struct bstr){\"hello\",6};\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void structmember_extern() { // extern struct => no false positive checkStructMemberUsage("extern struct AB\n"