-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[libc++][format] Applied [[nodiscard]] to more classes
#170808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
frederick-vs-ja
merged 3 commits into
llvm:main
from
H-G-Hristov:hgh/libcxx/nodiscard-to-format
Dec 12, 2025
Merged
[libc++][format] Applied [[nodiscard]] to more classes
#170808
frederick-vs-ja
merged 3 commits into
llvm:main
from
H-G-Hristov:hgh/libcxx/nodiscard-to-format
Dec 12, 2025
+29
−6
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[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.
Member
|
@llvm/pr-subscribers-libcxx Author: Hristo Hristov (H-G-Hristov) Changes
Some classes in Full diff: https://github.com/llvm/llvm-project/pull/170808.diff 4 Files Affected:
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
approved these changes
Dec 12, 2025
84 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[[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.