diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index efa5fd59d..c788ae992 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -827,7 +827,17 @@ void CheckClass::operatorEq() // use definition for check so we don't have to deal with qualification if (!(Token::Match(func->tokenDef->tokAt(-3), ";|}|{|public:|protected:|private: %type% &") && func->tokenDef->strAt(-2) == scope->className)) - operatorEqReturnError(func->tokenDef->tokAt(-1), scope->className); + { + // make sure we really have a copy assignment operator + if (Token::Match(func->tokenDef->tokAt(2), "const| %var% &")) + { + if (func->tokenDef->strAt(2) == "const" && + func->tokenDef->strAt(3) == scope->className) + operatorEqReturnError(func->tokenDef->tokAt(-1), scope->className); + else if (func->tokenDef->strAt(2) == scope->className) + operatorEqReturnError(func->tokenDef->tokAt(-1), scope->className); + } + } } } } diff --git a/test/testclass.cpp b/test/testclass.cpp index 94bc48a91..0ca29ac05 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -107,6 +107,7 @@ private: TEST_CASE(operatorEq1); TEST_CASE(operatorEq2); + TEST_CASE(operatorEq3); // ticket #3051 TEST_CASE(operatorEqRetRefThis1); TEST_CASE(operatorEqRetRefThis2); // ticket #1323 TEST_CASE(operatorEqRetRefThis3); // ticket #1405 @@ -306,6 +307,16 @@ private: ASSERT_EQUALS("[test.cpp:4]: (style) 'A::operator=' should return 'A &'\n", errout.str()); } + void operatorEq3() // ticket #3051 + { + checkOpertorEq("class A\n" + "{\n" + "public:\n" + " A * operator=(const A*);\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + // Check that operator Equal returns reference to this void checkOpertorEqRetRefThis(const char code[]) {