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

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


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

>From 61ca986bfb30a997defccbf09a335384ad594ce1 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                        |  8 ++--
 libcxx/include/streambuf                      |  8 ++--
 .../iostream.format/nodiscard.verify.cpp      | 46 +++++++++++++++++++
 .../stream.buffers/nodiscard.verify.cpp       | 29 ++++++++++++
 5 files changed, 84 insertions(+), 9 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..8d42f2fa08241 100644
--- a/libcxx/include/istream
+++ b/libcxx/include/istream
@@ -265,8 +265,8 @@ public:
   basic_istream& operator>>(void*& __p);
 
   // 27.7.1.3 Unformatted input:
-  _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
-  int_type get();
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
+  [[__nodiscard__]] int_type get();
 
   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type& __c) {
     int_type __ch = get();
@@ -298,7 +298,7 @@ public:
   _LIBCPP_HIDE_FROM_ABI basic_istream& ignore(streamsize __n, char_type __delim) {
     return ignore(__n, traits_type::to_int_type(__delim));
   }
-  int_type peek();
+  [[__nodiscard__]] int_type peek();
   basic_istream& read(char_type* __s, streamsize __n);
   streamsize readsome(char_type* __s, streamsize __n);
 
@@ -306,7 +306,7 @@ public:
   basic_istream& unget();
   int sync();
 
-  pos_type tellg();
+  [[__nodiscard__]] pos_type tellg();
   basic_istream& seekg(pos_type __pos);
   basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
 };
diff --git a/libcxx/include/streambuf b/libcxx/include/streambuf
index 7dc4e31cc2324..be790ffca1447 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(); });
 
@@ -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..940692d298896
--- /dev/null
+++ b/libcxx/test/libcxx/input.output/iostream.format/nodiscard.verify.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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]
+
+  // [istream]
+
+  {
+    std::basic_istream<char> stream;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    stream.gcount();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    stream.get();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    stream.peek();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    stream.tellg();
+  }
+
+  // [ostream]
+
+  {
+    std::basic_ostream<char> stream;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    stream.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..d95a753341ca8
--- /dev/null
+++ b/libcxx/test/libcxx/input.output/stream.buffers/nodiscard.verify.cpp
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sbuf.snextc();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  sbuf.sgetc();
+}



More information about the libcxx-commits mailing list