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
|
||||
* false positives we just bail out */
|
||||
|
@ -408,16 +408,16 @@ void CheckClass::constructors()
|
|||
}
|
||||
|
||||
// Check constructors
|
||||
checkConstructors(tok1, className[0]);
|
||||
checkConstructors(tok1, className[0], hasPrivateConstructor);
|
||||
|
||||
// Check assignment operators
|
||||
checkConstructors(tok1, "operator =");
|
||||
checkConstructors(tok1, "operator =", hasPrivateConstructor);
|
||||
|
||||
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);
|
||||
|
||||
|
@ -467,7 +467,7 @@ void CheckClass::checkConstructors(const Token *tok1, const char funcname[])
|
|||
operatorEqVarError(constructor_token, className, var->name);
|
||||
}
|
||||
else
|
||||
uninitVarError(constructor_token, className, var->name);
|
||||
uninitVarError(constructor_token, className, var->name, hasPrivateConstructor);
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
@ -103,11 +103,11 @@ private:
|
|||
Var *getVarList(const Token *tok1, bool withClasses);
|
||||
|
||||
// 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..
|
||||
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 unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname);
|
||||
void memsetClassError(const Token *tok, const std::string &memfunc);
|
||||
|
@ -119,7 +119,7 @@ private:
|
|||
void getErrorMessages()
|
||||
{
|
||||
noConstructorError(0, "classname");
|
||||
uninitVarError(0, "classname", "varname");
|
||||
uninitVarError(0, "classname", "varname", false);
|
||||
operatorEqVarError(0, "classname", "");
|
||||
unusedPrivateFunctionError(0, "classname", "funcname");
|
||||
memsetClassError(0, "memfunc");
|
||||
|
|
|
@ -347,6 +347,7 @@ private:
|
|||
|
||||
// Check..
|
||||
Settings settings;
|
||||
settings._showAll = true;
|
||||
CheckClass checkClass(&tokenizer, &settings, this);
|
||||
checkClass.constructors();
|
||||
}
|
||||
|
@ -532,7 +533,7 @@ private:
|
|||
" Foo() { }\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()
|
||||
|
@ -546,7 +547,8 @@ private:
|
|||
" Foo(int _i) { }\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