forked from qt-creator/qt-creator
Utils: Make take of SmallString explicit
Using a value after the move is undefined. For example, if you move an integer, it is not set to zero after the move. So you have to initialize it again. Adding the take method to SmallString makes this explicit. So if there is a case that you want to use the string again after a move, use take or initialize it yourself. Change-Id: I174116df9639d6a3f63c6f2c2a3bd184852927ce Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -195,17 +195,17 @@ public:
|
||||
|
||||
BasicSmallString &operator=(BasicSmallString &&other) noexcept
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
this->~BasicSmallString();
|
||||
|
||||
m_data = std::move(other.m_data);
|
||||
other.m_data.reset();
|
||||
swap(*this, other);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
BasicSmallString take() noexcept
|
||||
{
|
||||
auto tmp = std::move(*this);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
BasicSmallString clone() const
|
||||
{
|
||||
BasicSmallString clonedString(m_data);
|
||||
|
@@ -1560,7 +1560,7 @@ TEST(SmallString, ShortSmallStringMoveAssignment)
|
||||
|
||||
copy = std::move(text);
|
||||
|
||||
ASSERT_THAT(text, IsEmpty());
|
||||
ASSERT_THAT(text, SmallString("more text"));
|
||||
ASSERT_THAT(copy, SmallString("text"));
|
||||
}
|
||||
|
||||
@@ -1571,6 +1571,28 @@ TEST(SmallString, LongSmallStringMoveAssignment)
|
||||
|
||||
copy = std::move(text);
|
||||
|
||||
ASSERT_THAT(text, SmallString("more text"));
|
||||
ASSERT_THAT(copy, SmallString("this is a very very very very long text"));
|
||||
}
|
||||
|
||||
TEST(SmallString, ShortSmallStringTake)
|
||||
{
|
||||
SmallString text("text");
|
||||
SmallString copy("more text");
|
||||
|
||||
copy = text.take();
|
||||
|
||||
ASSERT_THAT(text, IsEmpty());
|
||||
ASSERT_THAT(copy, SmallString("text"));
|
||||
}
|
||||
|
||||
TEST(SmallString, LongSmallStringTake)
|
||||
{
|
||||
SmallString text("this is a very very very very long text");
|
||||
SmallString copy("more text");
|
||||
|
||||
copy = text.take();
|
||||
|
||||
ASSERT_THAT(text, IsEmpty());
|
||||
ASSERT_THAT(copy, SmallString("this is a very very very very long text"));
|
||||
}
|
||||
|
Reference in New Issue
Block a user