Improve check: Variable is not initialized in private constructor (C++11 or later)

This commit is contained in:
Daniel Marjamäki 2018-04-02 15:26:15 +02:00
parent 32a2060f14
commit e492932f19
3 changed files with 27 additions and 8 deletions

View File

@ -220,7 +220,7 @@ void CheckClass::constructors()
if (classNameUsed) if (classNameUsed)
operatorEqVarError(func->token, scope->className, var->name(), inconclusive); operatorEqVarError(func->token, scope->className, var->name(), inconclusive);
} else if (func->access != Private) { } else if (func->access != Private || _settings->standards.cpp >= Standards::CPP11) {
const Scope *varType = var->typeScope(); const Scope *varType = var->typeScope();
if (!varType || varType->type != Scope::eUnion) { if (!varType || varType->type != Scope::eUnion) {
if (func->type == Function::eConstructor && if (func->type == Function::eConstructor &&
@ -230,9 +230,9 @@ void CheckClass::constructors()
func->functionScope->classStart->link() == func->functionScope->classStart->next()) { func->functionScope->classStart->link() == func->functionScope->classStart->next()) {
// don't warn about user defined default constructor when there are other constructors // don't warn about user defined default constructor when there are other constructors
if (printInconclusive) if (printInconclusive)
uninitVarError(func->token, scope->className, var->name(), true); uninitVarError(func->token, func->access == Private, scope->className, var->name(), true);
} else } else
uninitVarError(func->token, scope->className, var->name(), inconclusive); uninitVarError(func->token, func->access == Private, scope->className, var->name(), inconclusive);
} }
} }
} }
@ -829,9 +829,9 @@ void CheckClass::noExplicitConstructorError(const Token *tok, const std::string
reportError(tok, Severity::style, "noExplicitConstructor", message + "\n" + verbose, CWE398, false); reportError(tok, Severity::style, "noExplicitConstructor", message + "\n" + verbose, CWE398, false);
} }
void CheckClass::uninitVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive) void CheckClass::uninitVarError(const Token *tok, bool isprivate, const std::string &classname, const std::string &varname, bool inconclusive)
{ {
reportError(tok, Severity::warning, "uninitMemberVar", "Member variable '" + classname + "::" + varname + "' is not initialized in the constructor.", CWE398, inconclusive); reportError(tok, Severity::warning, isprivate ? "uninitMemberVarPrivate" : "uninitMemberVar", "Member variable '" + classname + "::" + varname + "' is not initialized in the constructor.", CWE398, inconclusive);
} }
void CheckClass::operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive) void CheckClass::operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive)

View File

@ -164,7 +164,7 @@ private:
//void copyConstructorMallocError(const Token *cctor, const Token *alloc, const std::string& var_name); //void copyConstructorMallocError(const Token *cctor, const Token *alloc, const std::string& var_name);
void copyConstructorShallowCopyError(const Token *tok, const std::string& varname); void copyConstructorShallowCopyError(const Token *tok, const std::string& varname);
void noCopyConstructorError(const Token *tok, const std::string &classname, bool isStruct); void noCopyConstructorError(const Token *tok, const std::string &classname, bool isStruct);
void uninitVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive); void uninitVarError(const Token *tok, bool isprivate, const std::string &classname, const std::string &varname, bool inconclusive);
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive); void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive);
void unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname); void unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname);
void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type); void memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type);
@ -196,7 +196,7 @@ private:
//c.copyConstructorMallocError(nullptr, 0, "var"); //c.copyConstructorMallocError(nullptr, 0, "var");
c.copyConstructorShallowCopyError(nullptr, "var"); c.copyConstructorShallowCopyError(nullptr, "var");
c.noCopyConstructorError(nullptr, "class", false); c.noCopyConstructorError(nullptr, "class", false);
c.uninitVarError(nullptr, "classname", "varname", false); c.uninitVarError(nullptr, false, "classname", "varname", false);
c.operatorEqVarError(nullptr, "classname", emptyString, false); c.operatorEqVarError(nullptr, "classname", emptyString, false);
c.unusedPrivateFunctionError(nullptr, "classname", "funcname"); c.unusedPrivateFunctionError(nullptr, "classname", "funcname");
c.memsetError(nullptr, "memfunc", "classname", "class"); c.memsetError(nullptr, "memfunc", "classname", "class");

View File

@ -1092,6 +1092,18 @@ private:
void initvar_private_constructor() { void initvar_private_constructor() {
settings.standards.cpp = Standards::CPP11;
check("class Fred\n"
"{\n"
"private:\n"
" int var;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout.str());
settings.standards.cpp = Standards::CPP03;
check("class Fred\n" check("class Fred\n"
"{\n" "{\n"
"private:\n" "private:\n"
@ -2897,15 +2909,22 @@ private:
" char data[42];\n" " char data[42];\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void privateCtor1() { void privateCtor1() {
settings.standards.cpp = Standards::CPP03;
check("class Foo {\n" check("class Foo {\n"
" int foo;\n" " int foo;\n"
" Foo() { }\n" " Foo() { }\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
settings.standards.cpp = Standards::CPP11;
check("class Foo {\n"
" int foo;\n"
" Foo() { }\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Foo::foo' is not initialized in the constructor.\n", errout.str());
} }
void privateCtor2() { void privateCtor2() {