| 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. |
| 6 |
class PtrField { |
| 7 |
public: |
| 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 |
|
| 13 |
private: |
| 14 |
int *p; |
| 15 |
}; |
| 16 |
|
| 17 |
// With the option, check catches classes with trivial fields. |
| 18 |
class TrivialFields { |
| 19 |
public: |
| 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 |
|
| 25 |
private: |
| 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. |
| 34 |
class ClassWithoutFields { |
| 35 |
public: |
| 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 |
}; |