AK: Optional::operator=(Optional&&) should clear movee's has_value bit

We were forgetting to clear m_has_value in the Optional being moved
from when using operator=(Optional&&).
This commit is contained in:
Andreas Kling 2019-08-05 21:47:36 +02:00
parent cd08c8e1bf
commit 9553ecfe01
Notes: sideshowbarker 2024-07-19 12:51:56 +09:00
3 changed files with 34 additions and 0 deletions

View file

@ -104,6 +104,7 @@ public:
ASSERT(m_has_value);
T released_value = move(value());
value().~T();
m_has_value = false;
return released_value;
}

View file

@ -48,6 +48,9 @@ TestNonnullRefPtr: TestNonnullRefPtr.o $(SHARED_TEST_OBJS)
TestRefPtr: TestRefPtr.o $(SHARED_TEST_OBJS)
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestRefPtr.o $(SHARED_TEST_OBJS)
TestOptional: TestOptional.o $(SHARED_TEST_OBJS)
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestOptional.o $(SHARED_TEST_OBJS)
clean:
rm -f $(SHARED_TEST_OBJS)
rm -f $(PROGRAMS)

30
AK/Tests/TestOptional.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <AK/TestSuite.h>
#include <AK/AKString.h>
#include <AK/Optional.h>
TEST_CASE(basic_optional)
{
Optional<int> x;
EXPECT_EQ(x.has_value(), false);
x = 3;
EXPECT_EQ(x.has_value(), true);
EXPECT_EQ(x.value(), 3);
}
TEST_CASE(move_optional)
{
Optional<int> x;
EXPECT_EQ(x.has_value(), false);
x = 3;
EXPECT_EQ(x.has_value(), true);
EXPECT_EQ(x.value(), 3);
Optional<int> y;
y = move(x);
EXPECT_EQ(y.has_value(), true);
EXPECT_EQ(y.value(), 3);
EXPECT_EQ(x.has_value(), false);
}
TEST_MAIN(Optional)