#9391 Detecting uninited member variables with default ctor (#2249)

This commit is contained in:
blacktea 2019-10-08 20:33:07 +03:00 committed by Daniel Marjamäki
parent c98732dd8b
commit 08bc369296
2 changed files with 28 additions and 4 deletions

View File

@ -145,7 +145,7 @@ void CheckClass::constructors()
std::vector<Usage> usage(scope->varlist.size()); std::vector<Usage> usage(scope->varlist.size());
for (const Function &func : scope->functionList) { for (const Function &func : scope->functionList) {
if (!func.hasBody() || !(func.isConstructor() || func.type == Function::eOperatorEqual)) if (!(func.hasBody() || func.isDefault()) || !(func.isConstructor() || func.type == Function::eOperatorEqual))
continue; continue;
// Bail: If initializer list is not recognized as a variable or type then skip since parsing is incomplete // Bail: If initializer list is not recognized as a variable or type then skip since parsing is incomplete
@ -159,7 +159,8 @@ void CheckClass::constructors()
clearAllVar(usage); clearAllVar(usage);
std::list<const Function *> callstack; std::list<const Function *> callstack;
initializeVarList(func, callstack, scope, usage); if (func.hasBody())
initializeVarList(func, callstack, scope, usage);
// Check if any variables are uninitialized // Check if any variables are uninitialized
int count = -1; int count = -1;

View File

@ -385,14 +385,37 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void simple10() { // ticket #4388 void simple10() { // tickets #4388, #9391
check("class Fred {\n" check("class Fred {\n"
"public:\n" "public:\n"
" Fred() = default;\n" " Fred() = default;\n"
"private:\n" "private:\n"
" int x;\n" " int x;\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n", errout.str());
check("class Fred {\n"
"public:\n"
" Fred() = default;\n"
" int x;\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n", errout.str());
check("class Fred {\n"
"public:\n"
" Fred();\n"
" int x;\n"
"};\n"
"Fred::Fred()=default;");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n", errout.str());
check("class Fred {\n"
"public:\n"
" Fred() = default;\n"
"private:\n"
" int x = 0;\n"
"};");
ASSERT_EQUALS("", errout.str());
} }
void simple11() { // ticket #4536, #6214 void simple11() { // ticket #4536, #6214