[libcxx-commits] [libcxx] [libc++][iostream] Applied `[[nodiscard]]` (PR #173754)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sat Dec 27 21:26:19 PST 2025


https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/173754

`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.

- [x] `basic_streambuf`
- [x] `basic_istream`
- [x] `basic_ostream`
- [x] `basic_iostream`

- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/stream.buffers
- https://wg21.link/iostreamclass
- https://wg21.link/istream
- https://wg21.link/ostream

Towards #172124

>From 100cd8fb12d20a239a6000d3014b2d9252daf289 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Sat, 27 Dec 2025 07:25:10 +0200
Subject: [PATCH] [libc++][iostream] Applied `[[nodiscard]]`

`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.

- [x] `basic_streambuf`
- [x] `basic_istream`
- [x] `basic_ostream`
- [x] `basic_iostream`

- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/stream.buffers
- https://wg21.link/iostreamclass
- https://wg21.link/istream
- https://wg21.link/ostream

Towards #172124
---
 libcxx/include/__ostream/basic_ostream.h      |  2 +-
 libcxx/include/istream                        |  2 +-
 libcxx/include/streambuf                      | 10 ++---
 .../iostream.format/nodiscard.verify.cpp      | 41 +++++++++++++++++++
 .../stream.buffers/nodiscard.verify.cpp       | 23 +++++++++++
 5 files changed, 71 insertions(+), 7 deletions(-)
 create mode 100644 libcxx/test/libcxx/input.output/iostream.format/nodiscard.verify.cpp
 create mode 100644 libcxx/test/libcxx/input.output/stream.buffers/nodiscard.verify.cpp

diff --git a/libcxx/include/__ostream/basic_ostream.h b/libcxx/include/__ostream/basic_ostream.h
index effeef491f341..4fa8abc6fcf92 100644
--- a/libcxx/include/__ostream/basic_ostream.h
+++ b/libcxx/include/__ostream/basic_ostream.h
@@ -174,7 +174,7 @@ class basic_ostream : virtual public basic_ios<_CharT, _Traits> {
   basic_ostream& flush();
 
   // 27.7.2.5 seeks:
-  inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp();
+  [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp();
   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos);
   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
 
diff --git a/libcxx/include/istream b/libcxx/include/istream
index 7f15521f91a8a..22ea372714d0b 100644
--- a/libcxx/include/istream
+++ b/libcxx/include/istream
@@ -265,7 +265,7 @@ public:
   basic_istream& operator>>(void*& __p);
 
   // 27.7.1.3 Unformatted input:
-  _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
   int_type get();
 
   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type& __c) {
diff --git a/libcxx/include/streambuf b/libcxx/include/streambuf
index 7dc4e31cc2324..e1ac5fb2354f7 100644
--- a/libcxx/include/streambuf
+++ b/libcxx/include/streambuf
@@ -157,7 +157,7 @@ public:
     return __r;
   }
 
-  inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale getloc() const { return __loc_; }
+  [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale getloc() const { return __loc_; }
 
   // 27.6.2.2.2 buffer and positioning:
   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_streambuf* pubsetbuf(char_type* __s, streamsize __n) {
@@ -178,7 +178,7 @@ public:
 
   // Get and put areas:
   // 27.6.2.2.3 Get area:
-  inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize in_avail() {
+  [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize in_avail() {
     __check_invariants();
     auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
 
@@ -187,7 +187,7 @@ public:
     return showmanyc();
   }
 
-  inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type snextc() {
+  [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type snextc() {
     __check_invariants();
     auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
 
@@ -196,7 +196,7 @@ public:
     return sgetc();
   }
 
-  inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sbumpc() {
+  [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sbumpc() {
     __check_invariants();
     auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
 
@@ -207,7 +207,7 @@ public:
     return __c;
   }
 
-  inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sgetc() {
+  [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sgetc() {
     __check_invariants();
     auto __guard = std::__make_scope_guard([this] { this->__check_invariants(); });
 
diff --git a/libcxx/test/libcxx/input.output/iostream.format/nodiscard.verify.cpp b/libcxx/test/libcxx/input.output/iostream.format/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..652e325bcb81d
--- /dev/null
+++ b/libcxx/test/libcxx/input.output/iostream.format/nodiscard.verify.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <istream>
+// <ostream>
+// <iostream>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <iostream>
+
+void test() {
+  // [iostreamclass]
+
+  {
+    // none
+  }
+
+  // [istream]
+
+  {
+    std::basic_istream<char> s;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    sbuf.gcount();
+  }
+
+  // [ostream]
+
+  {
+    std::basic_ostream<char> s;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    s.tellp();
+  }
+}
diff --git a/libcxx/test/libcxx/input.output/stream.buffers/nodiscard.verify.cpp b/libcxx/test/libcxx/input.output/stream.buffers/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..3b47a9bdf00a6
--- /dev/null
+++ b/libcxx/test/libcxx/input.output/stream.buffers/nodiscard.verify.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <streambuf>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <streambuf>
+
+void test() {
+  std::basic_streambuf<char> sbuf;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sbuf.getloc();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sbuf.in_avail();
+}



More information about the libcxx-commits mailing list