[libcxx-commits] [libcxx] [libc++] Revert undesired `[[nodiscard]]` added to conversion functions (PR #173658)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jan 3 17:51:44 PST 2026


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/173658

>From db9f2b45220939dc455dec879f455903ac10ea9f Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Fri, 26 Dec 2025 19:45:18 +0800
Subject: [PATCH 1/2] [libc++] Revert undesired `[[nodiscard]]` added to
 conversion functions

Because
- no one should use them in explicit function call forms, and
- value-discarding casts to non-`void` are already diagnosed.
---
 libcxx/include/__ios/fpos.h                                | 2 +-
 libcxx/include/ios                                         | 4 ++--
 .../input.output/iostreams.base/nodiscard.verify.cpp       | 7 -------
 3 files changed, 3 insertions(+), 10 deletions(-)

diff --git a/libcxx/include/__ios/fpos.h b/libcxx/include/__ios/fpos.h
index 655158c6f2d89..af114421c839f 100644
--- a/libcxx/include/__ios/fpos.h
+++ b/libcxx/include/__ios/fpos.h
@@ -28,7 +28,7 @@ class fpos {
 public:
   _LIBCPP_HIDE_FROM_ABI fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
 
-  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI operator streamoff() const { return __off_; }
+  _LIBCPP_HIDE_FROM_ABI operator streamoff() const { return __off_; }
 
   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _StateT state() const { return __st_; }
   _LIBCPP_HIDE_FROM_ABI void state(_StateT __st) { __st_ = __st; }
diff --git a/libcxx/include/ios b/libcxx/include/ios
index 1055af0dc9130..9cf0aa8998ed1 100644
--- a/libcxx/include/ios
+++ b/libcxx/include/ios
@@ -575,9 +575,9 @@ public:
 #    ifdef _LIBCPP_CXX03_LANG
   // Preserve the ability to compare with literal 0,
   // and implicitly convert to bool, but not implicitly convert to int.
-  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI operator void*() const { return fail() ? nullptr : (void*)this; }
+  _LIBCPP_HIDE_FROM_ABI operator void*() const { return fail() ? nullptr : (void*)this; }
 #    else
-  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return !fail(); }
+  _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return !fail(); }
 #    endif
 
   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool operator!() const { return fail(); }
diff --git a/libcxx/test/libcxx/input.output/iostreams.base/nodiscard.verify.cpp b/libcxx/test/libcxx/input.output/iostreams.base/nodiscard.verify.cpp
index 84386fe3a725e..5806babebcfcd 100644
--- a/libcxx/test/libcxx/input.output/iostreams.base/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/input.output/iostreams.base/nodiscard.verify.cpp
@@ -46,11 +46,6 @@ void test() {
   {
     std::ios& ref = stream;
 
-#if TEST_STD_VER >= 11
-    ref.operator bool(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-#else
-    ref.operator void*(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-#endif
     !ref;             // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
     ref.rdstate();    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
     ref.good();       // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
@@ -70,8 +65,6 @@ void test() {
   {
     std::fpos<std::mbstate_t> pos;
 
-    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    pos.operator std::streamoff();
     pos.state(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
     pos + std::streamoff(0);

>From 541b77425804cec48dd6e682a9f79670c88ecd30 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Sun, 4 Jan 2026 09:51:23 +0800
Subject: [PATCH 2/2] Explain exclusion of conversion functions in
 `CodingGuidelines.rst`

---
 libcxx/docs/CodingGuidelines.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/docs/CodingGuidelines.rst b/libcxx/docs/CodingGuidelines.rst
index ff312d16cf7bb..97e6feef9d79b 100644
--- a/libcxx/docs/CodingGuidelines.rst
+++ b/libcxx/docs/CodingGuidelines.rst
@@ -165,6 +165,8 @@ 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.
+
 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
 ``.nodiscard.verify.cpp``.



More information about the libcxx-commits mailing list