Skip to content

[libc++] Document guidelines for applying [[nodiscard]] #84000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions libcxx/docs/DesignDocs/NodiscardPolicy.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
===================================================
Guidelines for applying ``[[nodiscard]]`` in libc++
===================================================

Libc++ adds ``[[nodiscard]]`` to functions in a lot of places. The standards
committee has decided to not have a recommended practice where to put them, so
this document lists where ``[[nodiscard]]`` should be applied in libc++.

When should ``[[nodiscard]]`` be added to functions?
====================================================

``[[nodiscard]]`` should be applied to functions

- where discarding the return value is most likely a correctness issue.
For example a locking constructor in ``unique_lock``.

- 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()``.

This can help spotting bugs easily which otherwise may take a very long time
to find.

- which return a constant. For example ``numeric_limits::min()``.
- which only observe a value. For example ``string::size()``.

Code that discards values from these kinds of functions is dead code. It can
either be removed, or the programmer meant to do something different.

- where discarding the value is most likely a misuse of the function. For
example ``find``.

This protects programmers from assuming too much about how the internals of
a function work, making code more robust in the presence of future
optimizations.

What should be done when adding ``[[nodiscard]]`` to a function?
================================================================

Applications of ``[[nodiscard]]`` are code like any other code, so we aim to
test them. This can be done with a ``.verify.cpp`` test. Many examples are
available. Just look for tests with the suffix ``.nodiscard.verify.cpp``.
1 change: 1 addition & 0 deletions libcxx/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Design Documents
DesignDocs/FeatureTestMacros
DesignDocs/FileTimeType
DesignDocs/HeaderRemovalPolicy
DesignDocs/NodiscardPolicy
DesignDocs/NoexceptPolicy
DesignDocs/PSTLIntegration
DesignDocs/ThreadingSupportAPI
Expand Down