Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #4168 (False positive: (style) struct or union member 'super::x…

…' is never used)
  • Loading branch information...
commit 382e1e95ef7ad532fcd50f80bc4cbba4429f5715 1 parent 7ecfb18
@danmar authored
Showing with 25 additions and 0 deletions.
  1. +12 −0 lib/checkunusedvar.cpp
  2. +13 −0 test/testunusedvar.cpp
View
12 lib/checkunusedvar.cpp
@@ -1082,6 +1082,18 @@ void CheckUnusedVar::checkStructMemberUsage()
if (Token::findmatch(tok, s.c_str()))
structname.clear();
+ // Bail out if instance is initialized with {}..
+ if (!structname.empty()) {
+ const std::string pattern1(structname + " %var% ;");
+ const Token *tok2 = tok;
+ while (NULL != (tok2 = Token::findmatch(tok2->next(), pattern1.c_str()))) {
+ if (Token::simpleMatch(tok2->tokAt(3), (tok2->strAt(1) + " = {").c_str())) {
+ structname.clear();
+ break;
+ }
+ }
+ }
+
// Try to prevent false positives when struct members are not used directly.
if (Token::findmatch(tok, (structname + " *").c_str()))
structname.clear();
View
13 test/testunusedvar.cpp
@@ -45,6 +45,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(structmember9); // #2017 - struct is inherited
TEST_CASE(structmember_extern); // No false positives for extern structs
TEST_CASE(structmember10);
+ TEST_CASE(structmember11); // #4168 - initialization with {} / passed by address to unknown function
TEST_CASE(localvar1);
TEST_CASE(localvar2);
@@ -333,6 +334,18 @@ class TestUnusedVar : public TestFixture {
ASSERT_EQUALS("", errout.str());
}
+ void structmember11() { // #4168
+ checkStructMemberUsage("struct abc { int x; };\n"
+ "struct abc s = {0};\n"
+ "void f() { do_something(&s); }\n");
+ ASSERT_EQUALS("", errout.str());
+
+ checkStructMemberUsage("struct abc { int x; };\n"
+ "struct abc s = {0};\n"
+ "void f() { }\n");
+ TODO_ASSERT_EQUALS("abc::x is not used", "", errout.str());
+ }
+
void structmember_extern() {
// extern struct => no false positive
checkStructMemberUsage("extern struct AB\n"
Please sign in to comment.
Something went wrong with that request. Please try again.