[libcxx-commits] [libcxx] [libc++][format] Applied `[[nodiscard]]` to more classes (PR #170808)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 5 00:25:19 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

<details>
<summary>Changes</summary>

`[[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.

---
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
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}}
 }

``````````

</details>


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


More information about the libcxx-commits mailing list