Hoangtuansu: Fixed #537 (false positive::operator= should return something)

This commit is contained in:
Daniel Marjamäki 2009-08-09 19:57:48 +02:00
parent 506fd63652
commit 3331cb834f
2 changed files with 67 additions and 1 deletions

View File

@ -640,8 +640,19 @@ void CheckClass::operatorEq()
{
const Token *tok = Token::findmatch(_tokenizer->tokens(), "void operator = (");
if (tok)
{
const Token *tok1 = tok;
while (tok1 && !Token::Match(tok1, "class %var%"))
{
if (tok1->str() == "public:")
{
operatorEqReturnError(tok);
break;
}
if (tok1->str() == "private:" || tok1->str() == "protected:")
break;
tok1 = tok1->previous();
}
}
}
//---------------------------------------------------------------------------

View File

@ -56,6 +56,61 @@ private:
TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2);
TEST_CASE(operatorEq1);
}
// Check the operator Equal
void checkOpertorEq(const char code[])
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Clear the error log
errout.str("");
// Check..
Settings settings;
CheckClass checkClass(&tokenizer, &settings, this);
checkClass.operatorEq();
}
void operatorEq1()
{
checkOpertorEq("class A\n"
"{\n"
"public:\n"
" void goo() {}"
" void operator=(const& A);\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (style) 'operator=' should return something\n", errout.str());
checkOpertorEq("class A\n"
"{\n"
"private:\n"
" void operator=(const& A);\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkOpertorEq("class A\n"
"{\n"
" void operator=(const& A);\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkOpertorEq("class A\n"
"{\n"
"public:\n"
" void goo() {}\n"
"private:\n"
" void operator=(const& A);\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
// Check that base classes have virtual destructors