Skip to content

Commit b2d98ef

Browse files
authored
More tests
1 parent 4f876eb commit b2d98ef

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

clang-tools-extra/test/clang-tidy/checkers/bugprone/unhandled-self-assignment.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,122 @@ bool operator!=(Foo2 &, Foo2 &) {
236236
};
237237
}
238238

239+
// Missing call to member `swap` function
240+
class AllocatorAwareClassNoSwap {
241+
// pointer member to trigger bugprone-unhandled-self-assignment
242+
void *foo = nullptr;
243+
244+
public:
245+
using allocator_type = std::pmr::allocator<>;
246+
247+
AllocatorAwareClassNoSwap(const AllocatorAwareClassNoSwap& other) {
248+
}
249+
250+
AllocatorAwareClassNoSwap(const AllocatorAwareClassNoSwap& other, const allocator_type& alloc) {
251+
}
252+
253+
AllocatorAwareClassNoSwap& operator=(const AllocatorAwareClassNoSwap& other) {
254+
// CHECK-MESSAGES: [[@LINE-1]]:32: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
255+
AllocatorAwareClassNoSwap tmp(other, get_allocator());
256+
return *this;
257+
}
258+
259+
allocator_type get_allocator() const {
260+
return allocator_type();
261+
}
262+
};
263+
264+
// "Wrong" type is passed to member `swap` function
265+
class AllocatorAwareClassSwapWrongArgType {
266+
// pointer member to trigger bugprone-unhandled-self-assignment
267+
void *foo = nullptr;
268+
269+
public:
270+
using allocator_type = std::pmr::allocator<>;
271+
272+
AllocatorAwareClassSwapWrongArgType(const AllocatorAwareClassSwapWrongArgType& other) {
273+
}
274+
275+
AllocatorAwareClassSwapWrongArgType(const AllocatorAwareClassSwapWrongArgType& other, const allocator_type& alloc) {
276+
}
277+
278+
AllocatorAwareClassSwapWrongArgType& operator=(const AllocatorAwareClassSwapWrongArgType& other) {
279+
// CHECK-MESSAGES: [[@LINE-1]]:42: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
280+
int tmp = 0;
281+
swap(tmp);
282+
283+
return *this;
284+
}
285+
286+
void swap(int&) noexcept {
287+
}
288+
289+
allocator_type get_allocator() const {
290+
return allocator_type();
291+
}
292+
};
293+
294+
295+
// Making ADL `swap` call but with wrong argument type
296+
class AllocatorAwareClassAdlSwapWrongArgType {
297+
// pointer member to trigger bugprone-unhandled-self-assignment
298+
void *foo = nullptr;
299+
300+
public:
301+
using allocator_type = std::pmr::allocator<>;
302+
303+
AllocatorAwareClassAdlSwapWrongArgType(const AllocatorAwareClassAdlSwapWrongArgType& other) {
304+
}
305+
306+
AllocatorAwareClassAdlSwapWrongArgType(const AllocatorAwareClassAdlSwapWrongArgType& other, const allocator_type& alloc) {
307+
}
308+
309+
AllocatorAwareClassAdlSwapWrongArgType& operator=(const AllocatorAwareClassAdlSwapWrongArgType& other) {
310+
// CHECK-MESSAGES: [[@LINE-1]]:45: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
311+
int tmp = 0;
312+
swap(*this, tmp);
313+
314+
return *this;
315+
}
316+
317+
allocator_type get_allocator() const {
318+
return allocator_type();
319+
}
320+
321+
friend void swap(AllocatorAwareClassAdlSwapWrongArgType&, int&) {
322+
}
323+
};
324+
325+
// `this` isn't passed to `swap`
326+
class AllocatorAwareClassAdlSwapWrongArgs {
327+
// pointer member to trigger bugprone-unhandled-self-assignment
328+
void *foo = nullptr;
329+
330+
public:
331+
using allocator_type = std::pmr::allocator<>;
332+
333+
AllocatorAwareClassAdlSwapWrongArgs(const AllocatorAwareClassAdlSwapWrongArgs& other) {
334+
}
335+
336+
AllocatorAwareClassAdlSwapWrongArgs(const AllocatorAwareClassAdlSwapWrongArgs& other, const allocator_type& alloc) {
337+
}
338+
339+
AllocatorAwareClassAdlSwapWrongArgs& operator=(const AllocatorAwareClassAdlSwapWrongArgs& other) {
340+
// CHECK-MESSAGES: [[@LINE-1]]:42: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
341+
AllocatorAwareClassAdlSwapWrongArgs tmp1(other, get_allocator());
342+
AllocatorAwareClassAdlSwapWrongArgs tmp2(*this, get_allocator());
343+
swap(tmp1, tmp2);
344+
return *this;
345+
}
346+
347+
allocator_type get_allocator() const {
348+
return allocator_type();
349+
}
350+
351+
friend void swap(AllocatorAwareClassAdlSwapWrongArgs&, AllocatorAwareClassAdlSwapWrongArgs&) {
352+
}
353+
};
354+
239355
///////////////////////////////////////////////////////////////////
240356
/// Test cases correctly ignored by the check.
241357

0 commit comments

Comments
 (0)