Fix ticket #309 (false positive::is not assigned a value in 'A::operator=')

http://sourceforge.net/apps/trac/cppcheck/ticket/309
This commit is contained in:
Reijo Tomperi 2009-06-17 00:58:16 +03:00
parent b7c9a4ed8c
commit f8b3a57682
2 changed files with 44 additions and 5 deletions

View File

@ -386,8 +386,28 @@ void CheckClass::CheckConstructors(const Token *tok1, const char funcname[])
continue; continue;
// It's non-static and it's not initialized => error // It's non-static and it's not initialized => error
if (strcmp(funcname, "operator =") == 0) if (Token::simpleMatch(constructor_token, "operator = (") ||
operatorEqVarError(constructor_token, className, var->name); Token::simpleMatch(constructor_token->tokAt(2), "operator = ("))
{
const Token *operStart = 0;
if (Token::simpleMatch(constructor_token, "operator = ("))
operStart = constructor_token->tokAt(2);
else
operStart = constructor_token->tokAt(4);
bool classNameUsed = false;
for (const Token *operTok = operStart; operTok != operStart->link(); operTok = operTok->next())
{
if (operTok->str() == className)
{
classNameUsed = true;
break;
}
}
if (classNameUsed)
operatorEqVarError(constructor_token, className, var->name);
}
else else
uninitVarError(constructor_token, className, var->name); uninitVarError(constructor_token, className, var->name);
} }

View File

@ -185,6 +185,25 @@ private:
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("class A\n"
"{\n"
" public:\n"
" A() : i(0), j(0) {}\n"
"\n"
" A &operator=(const int &value)\n"
" {\n"
" i = value;\n"
" return (*this);\n"
" }\n"
"\n"
" int i;\n"
" int j;\n"
"};\n"
"\n"
"int main() {}\n");
ASSERT_EQUALS("", errout.str());
} }
@ -194,7 +213,7 @@ private:
"{\n" "{\n"
"public:\n" "public:\n"
" Fred() { i = 0; }\n" " Fred() { i = 0; }\n"
" void operator=() { }\n" " void operator=(const Fred &fred) { }\n"
" int i;\n" " int i;\n"
"};\n"); "};\n");
ASSERT_EQUALS("[test.cpp:5]: (all style) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (all style) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
@ -206,9 +225,9 @@ private:
"{\n" "{\n"
"public:\n" "public:\n"
" Fred() { Init(); }\n" " Fred() { Init(); }\n"
" void operator=() { Init(); }\n" " void operator=(const Fred &fred) { Init(); }\n"
"private:\n" "private:\n"
" Init() { i = 0; }\n" " void Init() { i = 0; }\n"
" int i;\n" " int i;\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());