40 lines · plain
1.. title:: clang-tidy - bugprone-standalone-empty2 3bugprone-standalone-empty4=========================5 6Warns when ``empty()`` is used on a range and the result is ignored. Suggests7``clear()`` if it is an existing member function.8 9The ``empty()`` method on several common ranges returns a Boolean indicating10whether or not the range is empty, but is often mistakenly interpreted as11a way to clear the contents of a range. Some ranges offer a ``clear()``12method for this purpose. This check warns when a call to empty returns a13result that is ignored, and suggests replacing it with a call to ``clear()``14if it is available as a member function of the range.15 16For example, the following code could be used to indicate whether a range17is empty or not, but the result is ignored:18 19.. code-block:: c++20 21 std::vector<int> v;22 ...23 v.empty();24 25A 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 34Limitations35-----------36 37* Doesn't warn if ``empty()`` is defined and used with the ignore result in the38 class template definition (for example in the library implementation). These39 error cases can be caught with ``[[nodiscard]]`` attribute.40