Skip to content

Conversation

@H-G-Hristov
Copy link
Contributor

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

Some classes in <format> were already annotated. This patch completes the remaining.

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

- https://libcxx.llvm.org/CodingGuidelines.html

Some classes in `<format>` were already annotated. This patch completes the remaining.
@H-G-Hristov H-G-Hristov marked this pull request as ready for review December 5, 2025 08:24
@H-G-Hristov H-G-Hristov requested a review from a team as a code owner December 5, 2025 08:24
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 5, 2025

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

Changes

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

Some classes in &lt;format&gt; were already annotated. This patch completes the remaining.


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

4 Files Affected:

  • (modified) libcxx/include/__format/format_args.h (+1-1)
  • (modified) libcxx/include/__format/format_context.h (+3-3)
  • (modified) libcxx/include/__format/format_parse_context.h (+2-2)
  • (modified) libcxx/test/libcxx/diagnostics/format.nodiscard.verify.cpp (+23)
diff --git a/libcxx/include/__format/format_args.h b/libcxx/include/__format/format_args.h
index 9dd7a5ed9c094..f1b648a10ac4f 100644
--- a/libcxx/include/__format/format_args.h
+++ b/libcxx/include/__format/format_args.h
@@ -40,7 +40,7 @@ class basic_format_args {
     }
   }
 
-  _LIBCPP_HIDE_FROM_ABI basic_format_arg<_Context> get(size_t __id) const noexcept {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI basic_format_arg<_Context> get(size_t __id) const noexcept {
     if (__id >= __size_)
       return basic_format_arg<_Context>{};
 
diff --git a/libcxx/include/__format/format_context.h b/libcxx/include/__format/format_context.h
index 1771dd34b82fb..9732ea9bf7f85 100644
--- a/libcxx/include/__format/format_context.h
+++ b/libcxx/include/__format/format_context.h
@@ -80,17 +80,17 @@ class _LIBCPP_PREFERRED_NAME(format_context)
   template <class _Tp>
   using formatter_type = formatter<_Tp, _CharT>;
 
-  _LIBCPP_HIDE_FROM_ABI basic_format_arg<basic_format_context> arg(size_t __id) const noexcept {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI basic_format_arg<basic_format_context> arg(size_t __id) const noexcept {
     return __args_.get(__id);
   }
 #  if _LIBCPP_HAS_LOCALIZATION
-  _LIBCPP_HIDE_FROM_ABI std::locale locale() {
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI std::locale locale() {
     if (!__loc_)
       __loc_ = std::locale{};
     return *__loc_;
   }
 #  endif
-  _LIBCPP_HIDE_FROM_ABI iterator out() { return std::move(__out_it_); }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI iterator out() { return std::move(__out_it_); }
   _LIBCPP_HIDE_FROM_ABI void advance_to(iterator __it) { __out_it_ = std::move(__it); }
 
 private:
diff --git a/libcxx/include/__format/format_parse_context.h b/libcxx/include/__format/format_parse_context.h
index 67b90c7b7e62a..2eda9d7f1f972 100644
--- a/libcxx/include/__format/format_parse_context.h
+++ b/libcxx/include/__format/format_parse_context.h
@@ -41,8 +41,8 @@ class basic_format_parse_context {
   basic_format_parse_context(const basic_format_parse_context&)            = delete;
   basic_format_parse_context& operator=(const basic_format_parse_context&) = delete;
 
-  _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept { return __begin_; }
-  _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept { return __end_; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept { return __begin_; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept { return __end_; }
   _LIBCPP_HIDE_FROM_ABI constexpr void advance_to(const_iterator __it) { __begin_ = __it; }
 
   _LIBCPP_HIDE_FROM_ABI constexpr size_t next_arg_id() {
diff --git a/libcxx/test/libcxx/diagnostics/format.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/format.nodiscard.verify.cpp
index ede6988f8bb4e..cf2a5602ef6a1 100644
--- a/libcxx/test/libcxx/diagnostics/format.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/format.nodiscard.verify.cpp
@@ -14,7 +14,9 @@
 // UNSUPPORTED: c++03, c++11, c++14, c++17
 
 #include <format>
+#include <string>
 
+#include "test_format_context.h"
 #include "test_macros.h"
 
 #ifndef TEST_HAS_NO_LOCALIZATION
@@ -46,4 +48,25 @@ void test() {
 #  endif // TEST_HAS_NO_WIDE_CHARACTERS
 #endif   // TEST_HAS_NO_LOCALIZATION
   // clang-format on
+
+  std::basic_format_args args{std::make_format_args()};
+
+  args.get(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  using OutItT = std::back_insert_iterator<std::string>;
+  std::string str;
+  OutItT outIt{str};
+  using FormatCtxT = std::basic_format_context<OutItT, char>;
+  FormatCtxT fCtx  = test_format_context_create<OutItT, char>(outIt, std::make_format_args<FormatCtxT>());
+
+  fCtx.arg(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#if !defined(TEST_HAS_NO_LOCALIZATION)
+  fCtx.locale(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
+  fCtx.out(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  std::basic_format_parse_context<char> fpCtx{""};
+
+  fpCtx.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  fpCtx.end();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 }

@frederick-vs-ja frederick-vs-ja merged commit ecaf673 into llvm:main Dec 12, 2025
84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants