Skip to content

Commit e0e4617

Browse files
pypingouclaude
authored andcommitted
Fix self-move assignment warnings in tests
GCC 14's -Werror=self-move detects self-move assignments. These occur in tests that intentionally verify self-move safety of various types. Solution: use pointer indirection to hide self-moves from compiler analysis: auto* ptr = &obj; obj = std::move(*ptr); This preserves the runtime behavior (self-move) while preventing the compile-time warning. Applied to test files for: DynamicArray, intrusive_list, Promise, AllocatorWrapper, TypeErasurePointer, Scope, and LockFile. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Signed-off-by: Leonardo Rossetti <lrossett@redhat.com>
1 parent d3c8278 commit e0e4617

7 files changed

Lines changed: 21 additions & 11 deletions

File tree

score/concurrency/future/base_interruptible_promise_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ TYPED_TEST(BaseInterruptiblePromiseTest, MoveAssignmentAlsoMovesFutureRetrievalS
179179
TYPED_TEST(BaseInterruptiblePromiseTest, MoveAssignmentToSelf)
180180
{
181181
BaseInterruptiblePromise<TypeParam> moved_to_promise{};
182-
moved_to_promise = std::move(moved_to_promise);
182+
auto* promise_ptr = &moved_to_promise;
183+
moved_to_promise = std::move(*promise_ptr);
183184
ASSERT_TRUE(moved_to_promise.GetInterruptibleFuture().has_value());
184185
}
185186

score/containers/dynamic_array_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ TYPED_TEST(DynamicArrayTestFixture, SelfMoveAssign)
263263
DynamicArray<TrivialType, TypeParam> unit{kNonEmptyArraySize,
264264
GetAllocator<TrivialType, TypeParam>(this->memory_resource_)};
265265

266-
// when doing a self-move-assign
267-
unit = std::move(unit);
266+
// when doing a self-move-assign (use pointer indirection to avoid compiler warning)
267+
auto* unit_ptr = &unit;
268+
unit = std::move(*unit_ptr);
268269

269270
// expect, that the unit afterward still has the same size
270271
EXPECT_EQ(unit.size(), kNonEmptyArraySize);

score/containers/intrusive_list_test.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ TEST(IntrusiveList, MoveAssignmentTest)
552552
// NOLINTBEGIN(bugprone-use-after-move): testing correctness of implementation
553553

554554
List list;
555-
list = std::move(list);
555+
auto* list_ptr = &list;
556+
list = std::move(*list_ptr);
556557
CheckEmpty(list);
557558

558559
List list_from;
@@ -562,7 +563,8 @@ TEST(IntrusiveList, MoveAssignmentTest)
562563
list = std::move(list_from);
563564
CheckEmpty(list_from);
564565
CheckNonEmpty(list, 1, front_back, front_back);
565-
list = std::move(list);
566+
list_ptr = &list;
567+
list = std::move(*list_ptr);
566568
CheckNonEmpty(list, 1, front_back, front_back);
567569

568570
ListElement front;
@@ -572,7 +574,8 @@ TEST(IntrusiveList, MoveAssignmentTest)
572574
list = std::move(list_from);
573575
CheckEmpty(list_from);
574576
CheckNonEmpty(list, 2, front, back);
575-
list = std::move(list);
577+
list_ptr = &list;
578+
list = std::move(*list_ptr);
576579
CheckNonEmpty(list, 2, front, back);
577580

578581
constexpr std::size_t num_elements = 6;
@@ -581,7 +584,8 @@ TEST(IntrusiveList, MoveAssignmentTest)
581584
list = std::move(list_from);
582585
CheckEmpty(list_from);
583586
CheckNonEmpty(list, num_elements, elements[0], elements[num_elements - 1]);
584-
list = std::move(list);
587+
list_ptr = &list;
588+
list = std::move(*list_ptr);
585589
CheckNonEmpty(list, num_elements, elements[0], elements[num_elements - 1]);
586590
list.clear();
587591

score/language/safecpp/scoped_function/details/allocator_aware_type_erasure_pointer_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ TEST_F(AllocatorAwareTypeErasurePointerTest, CanMoveAssignSelfWithoutAdverseEffe
272272
DISABLE_WARNING_PUSH
273273
DISABLE_WARNING_SELF_MOVE
274274

275-
target = std::move(target);
275+
auto* target_ptr = &target;
276+
target = std::move(*target_ptr);
276277

277278
DISABLE_WARNING_POP
278279

score/language/safecpp/scoped_function/details/allocator_wrapper_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ TEST_F(AllocatorWrapperTest, CorrectlyHandlesSelfMoveAssignment)
109109
DISABLE_WARNING_PUSH
110110
DISABLE_WARNING_SELF_MOVE
111111

112-
allocator_wrapper = std::move(allocator_wrapper);
112+
auto* wrapper_ptr = &allocator_wrapper;
113+
allocator_wrapper = std::move(*wrapper_ptr);
113114

114115
DISABLE_WARNING_POP
115116

score/language/safecpp/scoped_function/scope_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ TYPED_TEST(ScopeTest, CanMoveAssignToItselfWithNoAdverseEffects)
126126
DISABLE_WARNING_PUSH
127127
DISABLE_WARNING_SELF_MOVE
128128

129-
scope = std::move(scope);
129+
auto* scope_ptr = &scope;
130+
scope = std::move(*scope_ptr);
130131

131132
DISABLE_WARNING_POP
132133

score/memory/shared/lock_file_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ TEST_F(LockFileMoveFixture, LockFileShouldNotRemoveFileWhenMoveAssigningToItself
554554
ASSERT_TRUE(lock_file_result.has_value());
555555

556556
// When we move assign the lock file to itself
557-
lock_file_result.value() = std::move(lock_file_result.value());
557+
auto* lock_file_ptr = &lock_file_result.value();
558+
lock_file_result.value() = std::move(*lock_file_ptr);
558559

559560
// Then the lock file won't be destroyed
560561
EXPECT_FALSE(close_called);

0 commit comments

Comments
 (0)