| 1 |
.. title:: clang-tidy - bugprone-unhandled-self-assignment |
| 2 |
|
| 3 |
bugprone-unhandled-self-assignment |
| 4 |
================================== |
| 5 |
|
| 6 |
`cert-oop54-cpp` redirects here as an alias for this check. For the CERT alias, |
| 7 |
the `WarnOnlyIfThisHasSuspiciousField` option is set to `false`. |
| 8 |
|
| 9 |
Finds user-defined copy assignment operators which do not protect the code |
| 10 |
against self-assignment either by checking self-assignment explicitly or |
| 11 |
using the copy-and-swap or the copy-and-move method. |
| 12 |
|
| 13 |
By default, this check searches only those classes which have any pointer or C array field |
| 14 |
to avoid false positives. In case of a pointer or a C array, it's likely that self-copy |
| 15 |
assignment breaks the object if the copy assignment operator was not written with care. |
| 16 |
|
| 17 |
See also: |
| 18 |
`OOP54-CPP. Gracefully handle self-copy assignment |
| 19 |
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP54-CPP.+Gracefully+handle+self-copy+assignment>`_ |
| 20 |
|
| 21 |
A copy assignment operator must prevent that self-copy assignment ruins the |
| 22 |
object state. A typical use case is when the class has a pointer field |
| 23 |
and the copy assignment operator first releases the pointed object and |
| 24 |
then tries to assign it: |
| 25 |
|
| 26 |
.. code-block:: c++ |
| 27 |
|
| 28 |
class T { |
| 29 |
int* p; |
| 30 |
|
| 31 |
public: |
| 32 |
T(const T &rhs) : p(rhs.p ? new int(*rhs.p) : nullptr) {} |
| 33 |
~T() { delete p; } |
| 34 |
|
| 35 |
// ... |
| 36 |
|
| 37 |
T& operator=(const T &rhs) { |
| 38 |
delete p; |
| 39 |
p = new int(*rhs.p); |
| 40 |
return *this; |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
There are two common C++ patterns to avoid this problem. The first is |
| 45 |
the self-assignment check: |
| 46 |
|
| 47 |
.. code-block:: c++ |
| 48 |
|
| 49 |
class T { |
| 50 |
int* p; |
| 51 |
|
| 52 |
public: |
| 53 |
T(const T &rhs) : p(rhs.p ? new int(*rhs.p) : nullptr) {} |
| 54 |
~T() { delete p; } |
| 55 |
|
| 56 |
// ... |
| 57 |
|
| 58 |
T& operator=(const T &rhs) { |
| 59 |
if(this == &rhs) |
| 60 |
return *this; |
| 61 |
|
| 62 |
delete p; |
| 63 |
p = new int(*rhs.p); |
| 64 |
return *this; |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
The second one is the copy-and-swap method when we create a temporary copy |
| 69 |
(using the copy constructor) and then swap this temporary object with ``this``: |
| 70 |
|
| 71 |
.. code-block:: c++ |
| 72 |
|
| 73 |
class T { |
| 74 |
int* p; |
| 75 |
|
| 76 |
public: |
| 77 |
T(const T &rhs) : p(rhs.p ? new int(*rhs.p) : nullptr) {} |
| 78 |
~T() { delete p; } |
| 79 |
|
| 80 |
// ... |
| 81 |
|
| 82 |
void swap(T &rhs) { |
| 83 |
using std::swap; |
| 84 |
swap(p, rhs.p); |
| 85 |
} |
| 86 |
|
| 87 |
T& operator=(const T &rhs) { |
| 88 |
T(rhs).swap(*this); |
| 89 |
return *this; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
There is a third pattern which is less common. Let's call it the copy-and-move method |
| 94 |
when we create a temporary copy (using the copy constructor) and then move this |
| 95 |
temporary object into ``this`` (needs a move assignment operator): |
| 96 |
|
| 97 |
.. code-block:: c++ |
| 98 |
|
| 99 |
class T { |
| 100 |
int* p; |
| 101 |
|
| 102 |
public: |
| 103 |
T(const T &rhs) : p(rhs.p ? new int(*rhs.p) : nullptr) {} |
| 104 |
~T() { delete p; } |
| 105 |
|
| 106 |
// ... |
| 107 |
|
| 108 |
T& operator=(const T &rhs) { |
| 109 |
T t = rhs; |
| 110 |
*this = std::move(t); |
| 111 |
return *this; |
| 112 |
} |
| 113 |
|
| 114 |
T& operator=(T &&rhs) { |
| 115 |
p = rhs.p; |
| 116 |
rhs.p = nullptr; |
| 117 |
return *this; |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
.. option:: WarnOnlyIfThisHasSuspiciousField |
| 122 |
|
| 123 |
When `true`, the check will warn only if the container class of the copy assignment operator |
| 124 |
has any suspicious fields (pointer or C array). This option is set to `true` by default. |