From 9c4bbd4c65554f09244525cd57ffbcc0ca942183 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Sat, 17 Jul 2010 12:26:05 +0200 Subject: [PATCH] Fixed #1730 (False negative in 'variable not initialized in ctor') --- lib/checkclass.cpp | 22 ++++++++++++++++------ test/testclass.cpp | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 0c158c104..554e09851 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -367,8 +367,16 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va else // there is a called member function, but it is not defined where we can find it, so we assume it initializes everything { // check if the function is part of this class.. - const Token *tok = Token::findmatch(_tokenizer->tokens(), ((isStruct ? std::string("struct ") : std::string("class ")) + classname + " {").c_str()); - for (tok = tok ? tok->tokAt(3) : 0; tok; tok = tok->next()) + const Token *tok = Token::findmatch(_tokenizer->tokens(), ((isStruct ? std::string("struct ") : std::string("class ")) + classname + " {|:").c_str()); + bool derived = false; + while (tok && tok->str() != "{") + { + if (tok->str() == ":") + derived = true; + tok = tok->next(); + } + + for (tok = tok ? tok->next() : 0; tok; tok = tok->next()) { if (tok->str() == "{") { @@ -382,13 +390,15 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va } else if (tok->str() == ftok->str() || tok->str() == "friend") { - tok = 0; - break; + if (tok->next()->str() == "(" || tok->str() == "friend") + { + tok = 0; + break; + } } } - // bail out.. - if (!tok) + if (!tok || derived) { for (Var *var = varlist; var; var = var->next) var->init = true; diff --git a/test/testclass.cpp b/test/testclass.cpp index d3c2cc2e5..b1854c17b 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -52,6 +52,7 @@ private: TEST_CASE(uninitVar6); TEST_CASE(uninitVar7); TEST_CASE(uninitVar8); + TEST_CASE(uninitVar9); // ticket #1730 TEST_CASE(uninitVarEnum); TEST_CASE(uninitVarStream); TEST_CASE(uninitVarTypedef); @@ -1583,6 +1584,21 @@ private: ASSERT_EQUALS("[test.cpp:8]: (style) Member variable 'Foo::a' is not assigned a value in 'Foo::operator='\n", errout.str()); } + void uninitVar9() // ticket #1730 + { + checkUninitVar("class Prefs {\n" + "private:\n" + " int xasd;\n" + "public:\n" + " Prefs(wxSize size);\n" + "};\n" + "Prefs::Prefs(wxSize size)\n" + "{\n" + " SetMinSize( wxSize( 48,48 ) );\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7]: (style) Member variable not initialized in the constructor 'Prefs::xasd'\n", errout.str()); + } + void uninitVarArray1() { checkUninitVar("class John\n"