Skip to content

Commit 0f2d722

Browse files
authored
Fix #15156: Enhancement: Add support for move-assignment for operatorEqToSelf (#8977)
1 parent d9a02a9 commit 0f2d722

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎lib/checkclass.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ void CheckClassImpl::operatorEqToSelf()
19241924
if (func.argumentList.empty())
19251925
continue;
19261926
const Token* typeTok = func.argumentList.front().typeEndToken();
1927-
while (typeTok->str() == "const" || typeTok->str() == "&" || typeTok->str() == "*")
1927+
while (typeTok->str() == "const" || typeTok->str() == "&" || typeTok->str() == "&&" || typeTok->str() == "*")
19281928
typeTok = typeTok->previous();
19291929
if (typeTok->str() != scope->className)
19301930
continue;

‎test/testclass.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class TestClass : public TestFixture {
8484
TEST_CASE(operatorEqToSelf7);
8585
TEST_CASE(operatorEqToSelf8); // ticket #2179
8686
TEST_CASE(operatorEqToSelf9); // ticket #2592
87+
TEST_CASE(operatorEqToSelf10); // ticket #15156
8788

8889
TEST_CASE(memsetOnStruct);
8990
TEST_CASE(memsetVector);
@@ -2674,6 +2675,23 @@ class TestClass : public TestFixture {
26742675
ASSERT_EQUALS("", errout_str());
26752676
}
26762677

2678+
void operatorEqToSelf10() {
2679+
checkOpertorEqToSelf(
2680+
"class Foo\n"
2681+
"{\n"
2682+
" int32_t * p;\n"
2683+
"public:\n"
2684+
" Foo& operator=( Foo && o ) noexcept\n"
2685+
" {\n"
2686+
" delete p;\n"
2687+
" p = o.p;\n"
2688+
" o.p = nullptr;\n"
2689+
" return *this;\n"
2690+
" }\n"
2691+
"};\n");
2692+
ASSERT_EQUALS("[test.cpp:5:10]: (warning) 'operator=' should check for assignment to self to avoid problems with dynamic memory. [operatorEqToSelf]\n", errout_str());
2693+
}
2694+
26772695
struct CheckVirtualDestructorOptions
26782696
{
26792697
bool inconclusive = false;

0 commit comments

Comments
 (0)