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 using formatter_type = formatter<_Tp, _CharT>; - _LIBCPP_HIDE_FROM_ABI basic_format_arg arg(size_t __id) const noexcept { + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI basic_format_arg 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 +#include +#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 str; + OutItT outIt{str}; + using FormatCtxT = std::basic_format_context; + FormatCtxT fCtx = test_format_context_create(outIt, std::make_format_args()); + + 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 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}} }