Improve check: Variable is not initialized in private constructor (C++11 or later)
This commit is contained in:
parent
32a2060f14
commit
e492932f19
|
@ -220,7 +220,7 @@ void CheckClass::constructors()
|
|||
|
||||
if (classNameUsed)
|
||||
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();
|
||||
if (!varType || varType->type != Scope::eUnion) {
|
||||
if (func->type == Function::eConstructor &&
|
||||
|
@ -230,9 +230,9 @@ void CheckClass::constructors()
|
|||
func->functionScope->classStart->link() == func->functionScope->classStart->next()) {
|
||||
// don't warn about user defined default constructor when there are other constructors
|
||||
if (printInconclusive)
|
||||
uninitVarError(func->token, scope->className, var->name(), true);
|
||||
uninitVarError(func->token, func->access == Private, scope->className, var->name(), true);
|
||||
} 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);
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
@ -164,7 +164,7 @@ private:
|
|||
//void copyConstructorMallocError(const Token *cctor, const Token *alloc, const std::string& var_name);
|
||||
void copyConstructorShallowCopyError(const Token *tok, const std::string& varname);
|
||||
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 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);
|
||||
|
@ -196,7 +196,7 @@ private:
|
|||
//c.copyConstructorMallocError(nullptr, 0, "var");
|
||||
c.copyConstructorShallowCopyError(nullptr, "var");
|
||||
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.unusedPrivateFunctionError(nullptr, "classname", "funcname");
|
||||
c.memsetError(nullptr, "memfunc", "classname", "class");
|
||||
|
|
|
@ -1092,6 +1092,18 @@ private:
|
|||
|
||||
|
||||
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"
|
||||
"{\n"
|
||||
"private:\n"
|
||||
|
@ -2897,15 +2909,22 @@ private:
|
|||
" char data[42];\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
}
|
||||
|
||||
void privateCtor1() {
|
||||
settings.standards.cpp = Standards::CPP03;
|
||||
check("class Foo {\n"
|
||||
" int foo;\n"
|
||||
" Foo() { }\n"
|
||||
"};");
|
||||
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() {
|
||||
|
|
Loading…
Reference in New Issue