xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/bugprone/standalone-empty.rst (revision 90f7db87936d967998f90c8b430e5243872223b3)
1.. title:: clang-tidy - bugprone-standalone-empty
2
3bugprone-standalone-empty
4=========================
5
6Warns when ``empty()`` is used on a range and the result is ignored. Suggests
7``clear()`` if it is an existing member function.
8
9The ``empty()`` method on several common ranges returns a Boolean indicating
10whether or not the range is empty, but is often mistakenly interpreted as
11a 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 a
13result 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 range
17is 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
33Limitations:
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.
38