Fixed #3746: Issue operatorEqToSelf error only if the operator takes an object of the class as argument.
This commit is contained in:
parent
eea92d96ec
commit
31a252b057
|
@ -865,6 +865,15 @@ void CheckClass::operatorEqToSelf()
|
||||||
|
|
||||||
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
|
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
|
||||||
if (func->type == Function::eOperatorEqual && func->hasBody) {
|
if (func->type == Function::eOperatorEqual && func->hasBody) {
|
||||||
|
// make sure that the operator takes an object of the same type as *this, otherwise we can't detect self-assignment checks
|
||||||
|
if (func->argumentList.empty())
|
||||||
|
continue;
|
||||||
|
const Token* typeTok = func->argumentList.front().typeEndToken();
|
||||||
|
while (typeTok->str() == "const" || typeTok->str() == "&" || typeTok->str() == "*")
|
||||||
|
typeTok = typeTok->previous();
|
||||||
|
if (typeTok->str() != scope->className)
|
||||||
|
continue;
|
||||||
|
|
||||||
// make sure return signature is correct
|
// make sure return signature is correct
|
||||||
if (Token::Match(func->tokenDef->tokAt(-3), ";|}|{|public:|protected:|private: %type% &") &&
|
if (Token::Match(func->tokenDef->tokAt(-3), ";|}|{|public:|protected:|private: %type% &") &&
|
||||||
func->tokenDef->strAt(-2) == scope->className) {
|
func->tokenDef->strAt(-2) == scope->className) {
|
||||||
|
@ -1339,7 +1348,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func)
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment/decrement (member variable?)..
|
// increment/decrement (member variable?)..
|
||||||
else if (Token::Match(tok1, "++|--")) {
|
else if (tok1->type() == Token::eIncDecOp) {
|
||||||
// var++ and var--
|
// var++ and var--
|
||||||
if (Token::Match(tok1->previous(), "%var%") &&
|
if (Token::Match(tok1->previous(), "%var%") &&
|
||||||
tok1->previous()->str() != "return") {
|
tok1->previous()->str() != "return") {
|
||||||
|
|
|
@ -1260,6 +1260,18 @@ private:
|
||||||
" return *this;\n"
|
" return *this;\n"
|
||||||
"};");
|
"};");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
checkOpertorEqToSelf(
|
||||||
|
"struct A {\n"
|
||||||
|
" char *s;\n"
|
||||||
|
" A& operator=(const B &b);\n"
|
||||||
|
"};\n"
|
||||||
|
"A& A::operator=(const B &b) {\n"
|
||||||
|
" free(s);\n"
|
||||||
|
" s = strdup(a.s);\n"
|
||||||
|
" return *this;\n"
|
||||||
|
"};");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void operatorEqToSelf6() {
|
void operatorEqToSelf6() {
|
||||||
|
|
Loading…
Reference in New Issue