From 1b1fd9d39c1c09863e449fd0f75389d1659706b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 22 Nov 2011 19:26:00 +0100 Subject: [PATCH] Fixed #3196 (False positive: member variable not initialized in constructor (union)) --- lib/checkclass.cpp | 16 ++++++++++++++++ test/testclass.cpp | 13 +++++++++++++ test/testconstructors.cpp | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 2845eee04..5020c07e4 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -86,6 +86,22 @@ void CheckClass::constructors() } } + // #3196 => bailout if there are nested unions + // TODO: handle union variables better + { + bool bailout = false; + for (std::list::const_iterator it = scope->nestedList.begin(); it != scope->nestedList.end(); ++it) { + const Scope * const nestedScope = *it; + if (nestedScope->type == Scope::eUnion) { + bailout = true; + break; + } + } + if (bailout) + continue; + } + + std::list::const_iterator func; std::vector usage(scope->varlist.size()); diff --git a/test/testclass.cpp b/test/testclass.cpp index 002d57834..c1d941b3f 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -80,6 +80,7 @@ private: TEST_CASE(uninitVarArray3D); TEST_CASE(uninitVarStruct1); // ticket #2172 TEST_CASE(uninitVarStruct2); // ticket #838 + TEST_CASE(uninitVarUnion); // ticket #3196 TEST_CASE(uninitMissingFuncDef); // can't expand function in constructor TEST_CASE(privateCtor1); // If constructor is private.. TEST_CASE(privateCtor2); // If constructor is private.. @@ -2539,6 +2540,18 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninitVarUnion() { // ticket #3196 + checkUninitVar("class Fred\n" + "{\n" + "private:\n" + " union { int a; int b; };\n" + "public:\n" + " Fred()\n" + " { a = 0; }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + void uninitMissingFuncDef() { // Unknown member function checkUninitVar("class Fred\n" diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 49d485349..5622248b4 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -805,7 +805,7 @@ private: " {\n" " }\n" "};\n"); - ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor.\n", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor.\n", "", errout.str()); }