CheckClass: Restore a few warnings about member initialization of classes

This commit is contained in:
Daniel Marjamäki 2018-06-29 22:54:12 +02:00
parent da2867c09a
commit 60ac463a79
2 changed files with 21 additions and 2 deletions

View File

@ -161,7 +161,7 @@ void CheckClass::constructors()
if (usage[count].assign || usage[count].init || var.isStatic()) if (usage[count].assign || usage[count].init || var.isStatic())
continue; continue;
if (var.valueType()->pointer == 0 && var.type() && var.type()->needInitialization == Type::False) if (var.valueType()->pointer == 0 && var.type() && var.type()->needInitialization == Type::False && var.type()->derivedFrom.empty())
continue; continue;
if (var.isConst() && func.isOperator()) // We can't set const members in assignment operator if (var.isConst() && func.isOperator()) // We can't set const members in assignment operator
@ -193,7 +193,7 @@ void CheckClass::constructors()
bool inconclusive = false; bool inconclusive = false;
// Don't warn about unknown types in copy constructors since we // Don't warn about unknown types in copy constructors since we
// don't know if they can be copied or not.. // don't know if they can be copied or not..
if ((func.type == Function::eCopyConstructor || func.type == Function::eOperatorEqual) && !isVariableCopyNeeded(var)) if ((func.type == Function::eCopyConstructor || func.type == Function::eMoveConstructor || func.type == Function::eOperatorEqual) && !isVariableCopyNeeded(var))
inconclusive = true; inconclusive = true;
if (!printInconclusive && inconclusive) if (!printInconclusive && inconclusive)

View File

@ -1356,6 +1356,25 @@ private:
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("class A : public std::vector<int>\n"
"{\n"
"public:\n"
" A(const A &a);\n"
"};\n"
"class B\n"
"{\n"
" A a;\n"
"public:\n"
" B(){}\n"
" B(const B&){}\n"
" B(B &&){}\n"
" const B& operator=(const B&){return *this;}\n"
"};", true);
ASSERT_EQUALS("[test.cpp:11]: (warning, inconclusive) Member variable 'B::a' is not initialized in the constructor.\n"
"[test.cpp:12]: (warning, inconclusive) Member variable 'B::a' is not initialized in the constructor.\n"
"[test.cpp:13]: (warning, inconclusive) Member variable 'B::a' is not assigned a value in 'B::operator='.\n",
errout.str());
check("class B\n" check("class B\n"
"{\n" "{\n"
"public:\n" "public:\n"