diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 024ed6c87..28114228e 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -730,6 +730,14 @@ void CheckClass::initializeVarList(const Function &func, std::listnext(); + bailout = true; + } + if (tok2->variable() && (bailout || tok2->variable()->isArray()) && tok2->strAt(1) != "[") + assignVar(tok2->str(), scope, usage); } // Assignment of array item of member variable? diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index fcd1f1ee5..01446dd6c 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -104,6 +104,8 @@ private: TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor TEST_CASE(initvar_func_ret_func_ptr); // ticket #4449 + TEST_CASE(initvar_alias); // #6921 + TEST_CASE(operatorEqSTL); TEST_CASE(uninitVar1); @@ -1389,6 +1391,54 @@ private: ASSERT_EQUALS("", errout.str()); } + void initvar_alias() { // #6921 + check("struct S {\n" + " int a;\n" + " S() {\n" + " int& pa = a;\n" + " pa = 4;\n" + " }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + check("struct S {\n" + " int a;\n" + " S() {\n" + " int* pa = &a;\n" + " *pa = 4;\n" + " }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + check("struct S {\n" + " int a[2];\n" + " S() {\n" + " int* pa = a;\n" + " for (int i = 0; i < 2; i++)\n" + " *pa++ = i;\n" + " }\n" + "};"); + ASSERT_EQUALS("", errout.str()); + + check("struct S {\n" + " int* a[2];\n" + " S() {\n" + " int* pa = a[1];\n" + " *pa = 0;\n" + " }\n" + "};"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'S::a' is not initialized in the constructor.\n", errout.str()); + + check("struct S {\n" + " int a;\n" + " S() {\n" + " int pa = a;\n" + " pa = 4;\n" + " }\n" + "};"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'S::a' is not initialized in the constructor.\n", errout.str()); + } + void operatorEqSTL() { check("class Fred\n" "{\n"