From a8dc17c1d9965eead00751e9470210b1bb4b01cc Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 26 Aug 2014 11:29:26 +0200 Subject: [PATCH] Fixed false positive #5566. --- lib/checkclass.cpp | 13 +++++++++++++ lib/checkuninitvar.cpp | 2 +- test/testunusedprivfunc.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 0b2612b21..8849b87e3 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -830,6 +830,19 @@ static bool checkFunctionUsage(const std::string& name, const Scope* scope) return true; } + for (std::list::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { + if (i->isStatic()) { + const Token* tok = Token::findmatch(scope->classEnd, "%varid% =|(|{", i->declarationId()); + if (tok) + tok = tok->tokAt(2); + while (tok && tok->str() != ";") { + if (tok->str() == name && (tok->strAt(-1) == "." || tok->strAt(-2) == scope->className)) + return true; + tok = tok->next(); + } + } + } + return false; // Unused in this scope } diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 207668531..ef9ac9e12 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1286,7 +1286,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, if (tok2->isName() && tok2->next()->isName()) condition += ' '; } - 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); + 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); } return true; } diff --git a/test/testunusedprivfunc.cpp b/test/testunusedprivfunc.cpp index 15084d627..511f29044 100644 --- a/test/testunusedprivfunc.cpp +++ b/test/testunusedprivfunc.cpp @@ -73,6 +73,8 @@ private: TEST_CASE(multiFile); TEST_CASE(unknownBaseTemplate); // ticket #2580 TEST_CASE(hierarchie_loop); // ticket 5590 + + TEST_CASE(staticVariable); //ticket #5566 } @@ -725,6 +727,30 @@ private: ASSERT_EQUALS("[test.cpp:10]: (style) Unused private function: 'InfiniteA::foo'\n", errout.str()); } + void staticVariable() { + check("class Foo {\n" + " static int i;\n" + " static int F() const { return 1; }\n" + "};\n" + "int Foo::i = Foo::F();"); + ASSERT_EQUALS("", errout.str()); + + check("class Foo {\n" + " static int i;\n" + " int F() const { return 1; }\n" + "};\n" + "Foo f;\n" + "int Foo::i = f.F();"); + ASSERT_EQUALS("", errout.str()); + + check("class Foo {\n" + " static int i;\n" + " static int F() const { return 1; }\n" + "};\n" + "int Foo::i = sth();" + "int i = F();"); + ASSERT_EQUALS("[test.cpp:3]: (style) Unused private function: 'Foo::F'\n", errout.str()); + } }; REGISTER_TEST(TestUnusedPrivateFunction)