class checking: It's a 'possible style' error if a private constructor is not initializing a member variable
This commit is contained in:
parent
701a7b0b41
commit
c2a37c5d69
|
@ -368,7 +368,7 @@ void CheckClass::constructors()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasPrivateConstructor)
|
if (hasPrivateConstructor && !_settings->_showAll)
|
||||||
{
|
{
|
||||||
/** @todo Handle private constructors. Right now to avoid
|
/** @todo Handle private constructors. Right now to avoid
|
||||||
* false positives we just bail out */
|
* false positives we just bail out */
|
||||||
|
@ -408,16 +408,16 @@ void CheckClass::constructors()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check constructors
|
// Check constructors
|
||||||
checkConstructors(tok1, className[0]);
|
checkConstructors(tok1, className[0], hasPrivateConstructor);
|
||||||
|
|
||||||
// Check assignment operators
|
// Check assignment operators
|
||||||
checkConstructors(tok1, "operator =");
|
checkConstructors(tok1, "operator =", hasPrivateConstructor);
|
||||||
|
|
||||||
tok1 = Token::findmatch(tok1->next(), pattern_class);
|
tok1 = Token::findmatch(tok1->next(), pattern_class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckClass::checkConstructors(const Token *tok1, const char funcname[])
|
void CheckClass::checkConstructors(const Token *tok1, const char funcname[], bool hasPrivateConstructor)
|
||||||
{
|
{
|
||||||
const char * const className = tok1->strAt(1);
|
const char * const className = tok1->strAt(1);
|
||||||
|
|
||||||
|
@ -467,7 +467,7 @@ void CheckClass::checkConstructors(const Token *tok1, const char funcname[])
|
||||||
operatorEqVarError(constructor_token, className, var->name);
|
operatorEqVarError(constructor_token, className, var->name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
uninitVarError(constructor_token, className, var->name);
|
uninitVarError(constructor_token, className, var->name, hasPrivateConstructor);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Var *var = varlist; var; var = var->next)
|
for (Var *var = varlist; var; var = var->next)
|
||||||
|
@ -893,9 +893,9 @@ void CheckClass::noConstructorError(const Token *tok, const std::string &classna
|
||||||
reportError(tok, Severity::style, "noConstructor", "The class '" + classname + "' has no constructor. Member variables not initialized.");
|
reportError(tok, Severity::style, "noConstructor", "The class '" + classname + "' has no constructor. Member variables not initialized.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckClass::uninitVarError(const Token *tok, const std::string &classname, const std::string &varname)
|
void CheckClass::uninitVarError(const Token *tok, const std::string &classname, const std::string &varname, bool hasPrivateConstructor)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style, "uninitVar", "Member variable not initialized in the constructor '" + classname + "::" + varname + "'");
|
reportError(tok, hasPrivateConstructor ? Severity::possibleStyle : Severity::style, "uninitVar", "Member variable not initialized in the constructor '" + classname + "::" + varname + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckClass::operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname)
|
void CheckClass::operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname)
|
||||||
|
|
|
@ -103,11 +103,11 @@ private:
|
||||||
Var *getVarList(const Token *tok1, bool withClasses);
|
Var *getVarList(const Token *tok1, bool withClasses);
|
||||||
|
|
||||||
// Check constructors for a specified class
|
// Check constructors for a specified class
|
||||||
void checkConstructors(const Token *tok1, const char funcname[]);
|
void checkConstructors(const Token *tok1, const char funcname[], bool hasPrivateConstructor);
|
||||||
|
|
||||||
// Reporting errors..
|
// Reporting errors..
|
||||||
void noConstructorError(const Token *tok, const std::string &classname);
|
void noConstructorError(const Token *tok, const std::string &classname);
|
||||||
void uninitVarError(const Token *tok, const std::string &classname, const std::string &varname);
|
void uninitVarError(const Token *tok, const std::string &classname, const std::string &varname, bool hasPrivateConstructor);
|
||||||
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname);
|
void operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname);
|
||||||
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 memsetClassError(const Token *tok, const std::string &memfunc);
|
void memsetClassError(const Token *tok, const std::string &memfunc);
|
||||||
|
@ -119,7 +119,7 @@ private:
|
||||||
void getErrorMessages()
|
void getErrorMessages()
|
||||||
{
|
{
|
||||||
noConstructorError(0, "classname");
|
noConstructorError(0, "classname");
|
||||||
uninitVarError(0, "classname", "varname");
|
uninitVarError(0, "classname", "varname", false);
|
||||||
operatorEqVarError(0, "classname", "");
|
operatorEqVarError(0, "classname", "");
|
||||||
unusedPrivateFunctionError(0, "classname", "funcname");
|
unusedPrivateFunctionError(0, "classname", "funcname");
|
||||||
memsetClassError(0, "memfunc");
|
memsetClassError(0, "memfunc");
|
||||||
|
|
|
@ -347,6 +347,7 @@ private:
|
||||||
|
|
||||||
// Check..
|
// Check..
|
||||||
Settings settings;
|
Settings settings;
|
||||||
|
settings._showAll = true;
|
||||||
CheckClass checkClass(&tokenizer, &settings, this);
|
CheckClass checkClass(&tokenizer, &settings, this);
|
||||||
checkClass.constructors();
|
checkClass.constructors();
|
||||||
}
|
}
|
||||||
|
@ -532,7 +533,7 @@ private:
|
||||||
" Foo() { }\n"
|
" Foo() { }\n"
|
||||||
"};\n");
|
"};\n");
|
||||||
|
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (possible style) Member variable not initialized in the constructor 'Foo::foo'\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void privateCtor2()
|
void privateCtor2()
|
||||||
|
@ -546,7 +547,8 @@ private:
|
||||||
" Foo(int _i) { }\n"
|
" Foo(int _i) { }\n"
|
||||||
"};\n");
|
"};\n");
|
||||||
|
|
||||||
TODO_ASSERT_EQUALS("[test.cpp:7] (style) Member variable not initialized in the constructor Foo::foo", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (possible style) Member variable not initialized in the constructor 'Foo::foo'\n"
|
||||||
|
"[test.cpp:7]: (possible style) Member variable not initialized in the constructor 'Foo::foo'\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue