operator=: changed error message when variable is not assigned

This commit is contained in:
Daniel Marjamäki 2009-03-31 19:00:56 +02:00
parent 14bdf1ee62
commit ebee7928e2
3 changed files with 16 additions and 6 deletions

View File

@ -371,7 +371,10 @@ void CheckClass::CheckConstructors(const Token *tok1, struct VAR *varlist, const
continue; continue;
// It's non-static and it's not initialized => error // It's non-static and it's not initialized => error
uninitVarError(constructor_token, className, var->name); if (strcmp(funcname, "operator =") == 0)
operatorEqVarError(constructor_token, className, var->name);
else
uninitVarError(constructor_token, className, var->name);
} }
for (struct VAR *var = varlist; var; var = var->next) for (struct VAR *var = varlist; var; var = var->next)
@ -590,7 +593,7 @@ void CheckClass::operatorEq()
const Token *tok = Token::findmatch(_tokenizer->tokens(), "void operator = ("); const Token *tok = Token::findmatch(_tokenizer->tokens(), "void operator = (");
if (tok) if (tok)
{ {
operatorEqError(tok); operatorEqReturnError(tok);
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -697,6 +700,11 @@ void CheckClass::uninitVarError(const Token *tok, const std::string &classname,
reportError(tok, "style", "uninitVar", "Member variable not initialized in the constructor '" + classname + "::" + varname + "'"); reportError(tok, "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)
{
reportError(tok, "all style", "operatorEqVarError", "Member variable '" + classname + "::" + varname + "' is not assigned a value in '" + classname + "::operator=" + "'");
}
void CheckClass::unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname) void CheckClass::unusedPrivateFunctionError(const Token *tok, const std::string &classname, const std::string &funcname)
{ {
reportError(tok, "style", "unusedPrivateFunction", "Unused private function '" + classname + "::" + funcname + "'"); reportError(tok, "style", "unusedPrivateFunction", "Unused private function '" + classname + "::" + funcname + "'");
@ -712,7 +720,7 @@ void CheckClass::memsetStructError(const Token *tok, const std::string &memfunc,
reportError(tok, "error", "memsetStruct", "Using '" + memfunc + "' on struct that contains a 'std::" + classname + "'"); reportError(tok, "error", "memsetStruct", "Using '" + memfunc + "' on struct that contains a 'std::" + classname + "'");
} }
void CheckClass::operatorEqError(const Token *tok) void CheckClass::operatorEqReturnError(const Token *tok)
{ {
reportError(tok, "style", "operatorEq", "'operator=' should return something"); reportError(tok, "style", "operatorEq", "'operator=' should return something");
} }

View File

@ -101,20 +101,22 @@ private:
// 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);
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);
void memsetStructError(const Token *tok, const std::string &memfunc, const std::string &classname); void memsetStructError(const Token *tok, const std::string &memfunc, const std::string &classname);
void operatorEqError(const Token *tok); void operatorEqReturnError(const Token *tok);
void virtualDestructorError(const Token *tok, const std::string &Base, const std::string &Derived); void virtualDestructorError(const Token *tok, const std::string &Base, const std::string &Derived);
void getErrorMessages() void getErrorMessages()
{ {
noConstructorError(0, "classname"); noConstructorError(0, "classname");
uninitVarError(0, "classname", "varname"); uninitVarError(0, "classname", "varname");
operatorEqVarError(0, "classname", "");
unusedPrivateFunctionError(0, "classname", "funcname"); unusedPrivateFunctionError(0, "classname", "funcname");
memsetClassError(0, "memfunc"); memsetClassError(0, "memfunc");
memsetStructError(0, "memfunc", "classname"); memsetStructError(0, "memfunc", "classname");
operatorEqError(0); operatorEqReturnError(0);
virtualDestructorError(0, "Base", "Derived"); virtualDestructorError(0, "Base", "Derived");
} }

View File

@ -197,7 +197,7 @@ private:
" void operator=() { }\n" " void operator=() { }\n"
" int i;\n" " int i;\n"
"};\n"); "};\n");
ASSERT_EQUALS(std::string("[test.cpp:5]: (style) Member variable not initialized in the constructor 'Fred::i'\n"), errout.str()); ASSERT_EQUALS(std::string("[test.cpp:5]: (all style) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n"), errout.str());
} }
void initvar_operator_eq3() void initvar_operator_eq3()