aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Bubke <marco.bubke@qt.io>2017-10-16 11:15:46 +0200
committerMarco Bubke <marco.bubke@qt.io>2017-10-20 14:36:37 +0000
commitce4d7e9d0e6bb5c761d41b9bdfbad0624dcbb994 (patch)
tree027dd9efc2de7b731a74ce2b1b1f70b6285241e6
parent1d53110402264e294592bb0c0e8a52546bdf426a (diff)
Utils: Fix smallstring move assignment
Before the string was simply swapped with the other string which can lead to an unexpected behavior for xvalues. Now the destructor of the source is called and it is default initialized. foo = std::move(bar); bar would now hold the value of foo. Change-Id: Ibea3f18333a168634b7faf2fdaf9b5b52c82d5cc Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
-rw-r--r--src/libs/utils/smallstring.h7
-rw-r--r--tests/unit/unittest/smallstring-test.cpp4
2 files changed, 7 insertions, 4 deletions
diff --git a/src/libs/utils/smallstring.h b/src/libs/utils/smallstring.h
index f358609258..02c18a2c60 100644
--- a/src/libs/utils/smallstring.h
+++ b/src/libs/utils/smallstring.h
@@ -172,14 +172,17 @@ public:
}
BasicSmallString(BasicSmallString &&other) noexcept
+ : m_data(other.m_data)
{
- m_data = other.m_data;
other.m_data = Internal::StringDataLayout<Size>();
}
BasicSmallString &operator=(BasicSmallString &&other) noexcept
{
- swap(*this, other);
+ this->~BasicSmallString();
+
+ m_data = other.m_data;
+ other.m_data = Internal::StringDataLayout<Size>();
return *this;
}
diff --git a/tests/unit/unittest/smallstring-test.cpp b/tests/unit/unittest/smallstring-test.cpp
index bee07f58ff..ca6534b558 100644
--- a/tests/unit/unittest/smallstring-test.cpp
+++ b/tests/unit/unittest/smallstring-test.cpp
@@ -1495,7 +1495,7 @@ TEST(SmallString, ShortSmallStringMoveAssignment)
copy = std::move(text);
- ASSERT_THAT(text, SmallString("more text"));
+ ASSERT_THAT(text, IsEmpty());
ASSERT_THAT(copy, SmallString("text"));
}
@@ -1506,7 +1506,7 @@ TEST(SmallString, LongSmallStringMoveAssignment)
copy = std::move(text);
- ASSERT_THAT(text, SmallString("more text"));
+ ASSERT_THAT(text, IsEmpty());
ASSERT_THAT(copy, SmallString("this is a very very very very long text"));
}