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

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 26 04:53:49 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

Because
- no one should use them in explicit function call forms, and
- value-discarding casts to non-`void` are already diagnosed.

---
Full diff: https://github.com/llvm/llvm-project/pull/173658.diff


3 Files Affected:

- (modified) libcxx/include/__ios/fpos.h (+1-1) 
- (modified) libcxx/include/ios (+2-2) 
- (modified) libcxx/test/libcxx/input.output/iostreams.base/nodiscard.verify.cpp (-7) 


``````````diff
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);

``````````

</details>


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


More information about the libcxx-commits mailing list