Skip to content

Commit a8dc17c

Browse files
committed
Fixed false positive cppcheck-opensource#5566.
1 parent 5e2ea8b commit a8dc17c

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

lib/checkclass.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,19 @@ static bool checkFunctionUsage(const std::string& name, const Scope* scope)
830830
return true;
831831
}
832832

833+
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) {
834+
if (i->isStatic()) {
835+
const Token* tok = Token::findmatch(scope->classEnd, "%varid% =|(|{", i->declarationId());
836+
if (tok)
837+
tok = tok->tokAt(2);
838+
while (tok && tok->str() != ";") {
839+
if (tok->str() == name && (tok->strAt(-1) == "." || tok->strAt(-2) == scope->className))
840+
return true;
841+
tok = tok->next();
842+
}
843+
}
844+
}
845+
833846
return false; // Unused in this scope
834847
}
835848

lib/checkuninitvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
12861286
if (tok2->isName() && tok2->next()->isName())
12871287
condition += ' ';
12881288
}
1289-
reportError(tok, Severity::debug, "debug", "bailout uninitialized variable checking for '" + var.nameToken()->str() + "'. can't determine if this condition can be false when previous condition is false: " + condition);
1289+
reportError(tok, Severity::debug, "debug", "bailout uninitialized variable checking for '" + var.name() + "'. can't determine if this condition can be false when previous condition is false: " + condition);
12901290
}
12911291
return true;
12921292
}

test/testunusedprivfunc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class TestUnusedPrivateFunction : public TestFixture {
7373
TEST_CASE(multiFile);
7474
TEST_CASE(unknownBaseTemplate); // ticket #2580
7575
TEST_CASE(hierarchie_loop); // ticket 5590
76+
77+
TEST_CASE(staticVariable); //ticket #5566
7678
}
7779

7880

@@ -725,6 +727,30 @@ class TestUnusedPrivateFunction : public TestFixture {
725727
ASSERT_EQUALS("[test.cpp:10]: (style) Unused private function: 'InfiniteA::foo'\n", errout.str());
726728
}
727729

730+
void staticVariable() {
731+
check("class Foo {\n"
732+
" static int i;\n"
733+
" static int F() const { return 1; }\n"
734+
"};\n"
735+
"int Foo::i = Foo::F();");
736+
ASSERT_EQUALS("", errout.str());
737+
738+
check("class Foo {\n"
739+
" static int i;\n"
740+
" int F() const { return 1; }\n"
741+
"};\n"
742+
"Foo f;\n"
743+
"int Foo::i = f.F();");
744+
ASSERT_EQUALS("", errout.str());
745+
746+
check("class Foo {\n"
747+
" static int i;\n"
748+
" static int F() const { return 1; }\n"
749+
"};\n"
750+
"int Foo::i = sth();"
751+
"int i = F();");
752+
ASSERT_EQUALS("[test.cpp:3]: (style) Unused private function: 'Foo::F'\n", errout.str());
753+
}
728754
};
729755

730756
REGISTER_TEST(TestUnusedPrivateFunction)

0 commit comments

Comments
 (0)