From a6b6022497cb454055be2983f31d663d7e7ec678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 14 Jul 2010 18:50:29 +0200 Subject: [PATCH] Fixed #1851 (false positive: Member variable not initialized int ctor) --- lib/checkclass.cpp | 7 +++++-- test/testclass.cpp | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 30f0efe43..5ab8b42d3 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -275,10 +275,13 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va initVar(varlist, ftok->strAt(1)); } - // Before a new statement there is "[{};)=]" or "else" - if (! Token::Match(ftok, "[{};()=]") && ftok->str() != "else") + // Before a new statement there is "[{};)=]" + if (! Token::Match(ftok, "[{};()=]")) continue; + if (Token::simpleMatch(ftok, "( !")) + ftok = ftok->next(); + // Using the operator= function to initialize all variables.. if (Token::simpleMatch(ftok->next(), "* this = ")) { diff --git a/test/testclass.cpp b/test/testclass.cpp index bff319f97..7dc527512 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -63,20 +63,21 @@ private: TEST_CASE(uninitVarArray5); TEST_CASE(uninitVarArray6); TEST_CASE(uninitVarArray2D); - TEST_CASE(uninitMissingFuncDef);// can't expand function in constructor - TEST_CASE(privateCtor1); // If constructor is private.. - TEST_CASE(privateCtor2); // If constructor is private.. - TEST_CASE(function); // Function is not variable - TEST_CASE(uninitVarHeader1); // Class is defined in header - TEST_CASE(uninitVarHeader2); // Class is defined in header - TEST_CASE(uninitVarHeader3); // Class is defined in header - TEST_CASE(uninitVarPublished); // Borland C++: Variables in the published section are auto-initialized - TEST_CASE(uninitProperty); // Borland C++: No FP for properties - TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]' - TEST_CASE(uninitFunction1); // No FP when initialized in function - TEST_CASE(uninitFunction2); // No FP when initialized in function - TEST_CASE(uninitSameClassName); // No FP when two classes have the same name - TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions + TEST_CASE(uninitMissingFuncDef); // can't expand function in constructor + TEST_CASE(privateCtor1); // If constructor is private.. + TEST_CASE(privateCtor2); // If constructor is private.. + TEST_CASE(function); // Function is not variable + TEST_CASE(uninitVarHeader1); // Class is defined in header + TEST_CASE(uninitVarHeader2); // Class is defined in header + TEST_CASE(uninitVarHeader3); // Class is defined in header + TEST_CASE(uninitVarPublished); // Borland C++: Variables in the published section are auto-initialized + TEST_CASE(uninitProperty); // Borland C++: No FP for properties + TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]' + TEST_CASE(uninitFunction1); // No FP when initialized in function + TEST_CASE(uninitFunction2); // No FP when initialized in function + TEST_CASE(uninitFunction3); // No FP when initialized in function + TEST_CASE(uninitSameClassName); // No FP when two classes have the same name + TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions TEST_CASE(noConstructor1); TEST_CASE(noConstructor2); @@ -2002,6 +2003,21 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninitFunction3() + { + checkUninitVar("class Fred\n" + "{\n" + "public:\n" + " Fred() { if (!init()); }\n" + "\n" + " bool init()\n" + " { d = 0; return true; }\n" + "\n" + " double d;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void uninitSameClassName() { checkUninitVar("class B\n"