1// RUN: %check_clang_tidy %s bugprone-unhandled-self-assignment %t -- \
2// RUN: -config="{CheckOptions: \
3// RUN: {bugprone-unhandled-self-assignment.WarnOnlyIfThisHasSuspiciousField: false}}"
4
5// Classes with pointer field are still caught.
6class PtrField {
7public:
8 PtrField &operator=(const PtrField &object) {
9 // CHECK-MESSAGES: [[@LINE-1]]:13: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
10 return *this;
11 }
12
13private:
14 int *p;
15};
16
17// With the option, check catches classes with trivial fields.
18class TrivialFields {
19public:
20 TrivialFields &operator=(const TrivialFields &object) {
21 // CHECK-MESSAGES: [[@LINE-1]]:18: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
22 return *this;
23 }
24
25private:
26 int m;
27 float f;
28 double d;
29 bool b;
30};
31
32// The check warns also when there is no field at all.
33// In this case, user-defined copy assignment operator is useless anyway.
34class ClassWithoutFields {
35public:
36 ClassWithoutFields &operator=(const ClassWithoutFields &object) {
37 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: operator=() does not handle self-assignment properly [bugprone-unhandled-self-assignment]
38 return *this;
39 }
40};
41

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/unhandled-self-assignment-warn-only-if-this-has-suspicious-field.cpp