Refactorization in checkclass.cpp:

- Improved handling of pointers and constants in constructor checking (-> Fixed #3801)
This commit is contained in:
PKEuS 2012-05-14 12:50:23 -07:00
parent 1dee3b04b9
commit 42fd19fb37
2 changed files with 69 additions and 3 deletions

View File

@ -110,11 +110,11 @@ void CheckClass::constructors()
if (usage[count].assign || usage[count].init || var->isStatic())
continue;
if (var->isConst() && var->nameToken()->previous()->str() != "*")
if (var->isConst() && func->isOperator) // We can't set const members in assignment operator
continue;
// Check if this is a class constructor
if (var->isClass() && func->type == Function::eConstructor) {
if (!var->isPointer() && var->isClass() && func->type == Function::eConstructor) {
// Unknown type so assume it is initialized
if (!var->type())
continue;
@ -126,7 +126,7 @@ void CheckClass::constructors()
}
// Check if type can't be copied
if (var->type() && canNotCopy(var->type()))
if (!var->isPointer() && var->type() && canNotCopy(var->type()))
continue;
// It's non-static and it's not initialized => error

View File

@ -100,6 +100,8 @@ private:
TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions
TEST_CASE(uninitJava); // Java: no FP when variable is initialized in declaration
TEST_CASE(uninitVarOperatorEqual); // ticket #2415
TEST_CASE(uninitVarPointer); // ticket #3801
TEST_CASE(uninitConstVar);
TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2);
@ -3142,6 +3144,70 @@ private:
"[test.cpp:5]: (warning) Member variable 'A::a' is not assigned a value in 'A::operator='\n", errout.str());
}
void uninitVarPointer() { // #3801
checkUninitVar("struct A {\n"
" int a;\n"
"};\n"
"struct B {\n"
" A* a;\n"
" B() { }\n"
"};");
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'B::a' is not initialized in the constructor.\n", errout.str());
checkUninitVar("struct A;\n"
"struct B {\n"
" A* a;\n"
" B() { }\n"
"};");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor.\n", errout.str());
checkUninitVar("struct A;\n"
"struct B {\n"
" const A* a;\n"
" B() { }\n"
"};");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor.\n", errout.str());
checkUninitVar("struct A;\n"
"struct B {\n"
" A* const a;\n"
" B() { }\n"
"};");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor.\n", errout.str());
}
void uninitConstVar() {
checkUninitVar("struct A;\n"
"struct B {\n"
" A* const a;\n"
" B() { }\n"
" B(B& b) { }\n"
"};");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor.\n"
"[test.cpp:5]: (warning) Member variable 'B::a' is not initialized in the constructor.\n", errout.str());
checkUninitVar("struct A;\n"
"struct B {\n"
" A* const a;\n"
" B& operator=(const B& r) { }\n"
"};");
TODO_ASSERT_EQUALS("", "[test.cpp:4]: (warning) Member variable 'B::a' is not assigned a value in 'B::operator='\n", errout.str()); // #3804
checkUninitVar("struct B {\n"
" const int a;\n"
" B() { }\n"
" B(B& b) { }\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'B::a' is not initialized in the constructor.\n"
"[test.cpp:4]: (warning) Member variable 'B::a' is not initialized in the constructor.\n", errout.str());
checkUninitVar("struct B {\n"
" const int a;\n"
" B& operator=(const B& r) { }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void checkNoConstructor(const char code[]) {
// Clear the error log
errout.str("");