[libcxx-commits] [libcxx] [libc++][RFC] Document when to apply [[nodiscard]] (PR #84000)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Mar 28 08:45:21 PDT 2024


================
@@ -0,0 +1,34 @@
+======================================
+``[[nodiscard]]`` extensions in libc++
+======================================
+
+Libc++ adds ``[[nodiscard]]`` to functions in a lot more places than the
+standard does. Any applications of ``[[nodiscard]]`` that aren't required by the
+standard written as ``_LIBCPP_NODISCARD_EXT`` to make it possible to disable
+them. This can be done by defining ``_LIBCPP_DISABLE_NODISCARD_EXT``.
+
+When should ``[[nodiscard]]`` be added to functions?
+====================================================
+
+``[[nodiscard]]`` should be applied to functions
+
+- where it is most likely a correctness issue when discarding the return value.
+  For example a locking constructor in ``unique_lock``.
+- where most likely something similar was meant if the return value is
+  discarded. For example ``vector::empty()``, which probably should have
+  been ``clear()``.
----------------
ldionne wrote:

```suggestion
- where discarding the return value likely points to the user wanting to do something different. For example ``vector::empty()``, which probably should have
  been ``vector::clear()``.
```

https://github.com/llvm/llvm-project/pull/84000


More information about the libcxx-commits mailing list