| 1 |
.. title:: clang-tidy - bugprone-inaccurate-erase |
| 2 |
|
| 3 |
bugprone-inaccurate-erase |
| 4 |
========================= |
| 5 |
|
| 6 |
|
| 7 |
Checks for inaccurate use of the ``erase()`` method. |
| 8 |
|
| 9 |
Algorithms like ``remove()`` do not actually remove any element from the |
| 10 |
container but return an iterator to the first redundant element at the end |
| 11 |
of the container. These redundant elements must be removed using the |
| 12 |
``erase()`` method. This check warns when not all of the elements will be |
| 13 |
removed due to using an inappropriate overload. |
| 14 |
|
| 15 |
For example, the following code erases only one element: |
| 16 |
|
| 17 |
.. code-block:: c++ |
| 18 |
|
| 19 |
std::vector<int> xs; |
| 20 |
... |
| 21 |
xs.erase(std::remove(xs.begin(), xs.end(), 10)); |
| 22 |
|
| 23 |
Call the two-argument overload of ``erase()`` to remove the subrange: |
| 24 |
|
| 25 |
.. code-block:: c++ |
| 26 |
|
| 27 |
std::vector<int> xs; |
| 28 |
... |
| 29 |
xs.erase(std::remove(xs.begin(), xs.end(), 10), xs.end()); |