Fixed #4119 (false positive with operator= when implemented in terms of swap)

This commit is contained in:
Daniel Marjamäki 2012-09-22 08:50:36 +02:00
parent a65f427195
commit 6d928a2ff7
2 changed files with 22 additions and 0 deletions

View File

@ -398,6 +398,12 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
break; break;
} }
// Using swap to assign all variables..
if (func.type == Function::eOperatorEqual && Token::Match(ftok, "[;{}] %var% (") && Token::Match(ftok->linkAt(2), ") . %var% ( *| this ) ;")) {
assignAllVar(usage);
break;
}
// Calling member variable function? // Calling member variable function?
if (Token::Match(ftok->next(), "%var% . %var% (")) { if (Token::Match(ftok->next(), "%var% . %var% (")) {
std::list<Variable>::const_iterator var; std::list<Variable>::const_iterator var;

View File

@ -65,6 +65,7 @@ private:
TEST_CASE(initvar_operator_eq2); // BUG 2190376 TEST_CASE(initvar_operator_eq2); // BUG 2190376
TEST_CASE(initvar_operator_eq3); TEST_CASE(initvar_operator_eq3);
TEST_CASE(initvar_operator_eq4); // ticket #2204 TEST_CASE(initvar_operator_eq4); // ticket #2204
TEST_CASE(initvar_operator_eq5); // ticket #4119
TEST_CASE(initvar_same_classname); // BUG 2208157 TEST_CASE(initvar_same_classname); // BUG 2208157
TEST_CASE(initvar_chained_assign); // BUG 2270433 TEST_CASE(initvar_chained_assign); // BUG 2270433
TEST_CASE(initvar_2constructors); // BUG 2270353 TEST_CASE(initvar_2constructors); // BUG 2270353
@ -422,6 +423,21 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void initvar_operator_eq5() { // #4119 - false positive when using swap to assign variables
check("class Fred {\n"
" int i;\n"
"public:\n"
" Fred() : i(5) { }\n"
" ~Fred() { }\n"
" Fred(const Fred &fred) : i(fred.i) { }\n"
" Fred & operator=(const Fred &rhs) {\n"
" Fred(rhs).swap(*this);\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_same_classname() { void initvar_same_classname() {
// Bug 2208157 - False positive: Uninitialized variable, same class name // Bug 2208157 - False positive: Uninitialized variable, same class name