| 1 |
.. title:: clang-tidy - bugprone-standalone-empty |
| 2 |
|
| 3 |
bugprone-standalone-empty |
| 4 |
========================= |
| 5 |
|
| 6 |
Warns when ``empty()`` is used on a range and the result is ignored. Suggests |
| 7 |
``clear()`` if it is an existing member function. |
| 8 |
|
| 9 |
The ``empty()`` method on several common ranges returns a Boolean indicating |
| 10 |
whether or not the range is empty, but is often mistakenly interpreted as |
| 11 |
a way to clear the contents of a range. Some ranges offer a ``clear()`` |
| 12 |
method for this purpose. This check warns when a call to empty returns a |
| 13 |
result that is ignored, and suggests replacing it with a call to ``clear()`` |
| 14 |
if it is available as a member function of the range. |
| 15 |
|
| 16 |
For example, the following code could be used to indicate whether a range |
| 17 |
is empty or not, but the result is ignored: |
| 18 |
|
| 19 |
.. code-block:: c++ |
| 20 |
|
| 21 |
std::vector<int> v; |
| 22 |
... |
| 23 |
v.empty(); |
| 24 |
|
| 25 |
A call to ``clear()`` would appropriately clear the contents of the range: |
| 26 |
|
| 27 |
.. code-block:: c++ |
| 28 |
|
| 29 |
std::vector<int> v; |
| 30 |
... |
| 31 |
v.clear(); |
| 32 |
|
| 33 |
Limitations: |
| 34 |
|
| 35 |
* Doesn't warn if ``empty()`` is defined and used with the ignore result in the |
| 36 |
class template definition (for example in the library implementation). These |
| 37 |
error cases can be caught with ``[[nodiscard]]`` attribute. |