[libc++][docs] Update [[nodiscard]] coding guidelines (#178384)

as requested in
https://github.com/llvm/llvm-project/pull/176936#pullrequestreview-3706775444

Clarifies when not to apply `[[nodiscard]]`.


https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant

---------

Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
Co-authored-by: Hristo Hristov <zingam@outlook.com>
This commit is contained in:
Hristo Hristov 2026-02-09 18:44:45 +02:00 committed by GitHub
parent 47fb7cd732
commit e6c73eb0c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -175,7 +175,10 @@ have a recommended practice where to put them, so libc++ applies it whenever it
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.
``[[nodiscard]]`` should not be applied to conversion functions because Clang already diagnoses unused cast results.
``[[nodiscard]]`` should not be applied to functions if Clang already diagnoses unused results, for example:
- conversion functions
- equality operators
- relational operators
Applications of ``[[nodiscard]]`` are code like any other code, so we aim to test them on public interfaces. This can be
done with a ``.verify.cpp`` test. Many examples are available. Just look for tests with the suffix