-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc++][queue] Applied [[nodiscard]]
#169469
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 2 commits into
llvm:main
from
H-G-Hristov:hgh/libcxx/nodiscard-to-queue-priority_queue
Nov 27, 2025
Merged
[libc++][queue] Applied [[nodiscard]]
#169469
frederick-vs-ja
merged 2 commits into
llvm:main
from
H-G-Hristov:hgh/libcxx/nodiscard-to-queue-priority_queue
Nov 27, 2025
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
Member
|
@llvm/pr-subscribers-libcxx Author: Hristo Hristov (H-G-Hristov) Changes
Full diff: https://github.com/llvm/llvm-project/pull/169469.diff 2 Files Affected:
diff --git a/libcxx/include/queue b/libcxx/include/queue
index b4b79fb25a35f..63ef17b21e188 100644
--- a/libcxx/include/queue
+++ b/libcxx/include/queue
@@ -376,12 +376,12 @@ public:
# endif // _LIBCPP_CXX03_LANG
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const { return c.empty(); }
- _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
- _LIBCPP_HIDE_FROM_ABI reference front() { return c.front(); }
- _LIBCPP_HIDE_FROM_ABI const_reference front() const { return c.front(); }
- _LIBCPP_HIDE_FROM_ABI reference back() { return c.back(); }
- _LIBCPP_HIDE_FROM_ABI const_reference back() const { return c.back(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference front() { return c.front(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference front() const { return c.front(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference back() { return c.back(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference back() const { return c.back(); }
_LIBCPP_HIDE_FROM_ABI void push(const value_type& __v) { c.push_back(__v); }
# ifndef _LIBCPP_CXX03_LANG
@@ -401,13 +401,11 @@ public:
template <class... _Args>
_LIBCPP_HIDE_FROM_ABI
# if _LIBCPP_STD_VER >= 17
- decltype(auto)
- emplace(_Args&&... __args) {
+ decltype(auto) emplace(_Args&&... __args) {
return c.emplace_back(std::forward<_Args>(__args)...);
}
# else
- void
- emplace(_Args&&... __args) {
+ void emplace(_Args&&... __args) {
c.emplace_back(std::forward<_Args>(__args)...);
}
# endif
@@ -664,8 +662,10 @@ public:
# endif
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI bool empty() const { return c.empty(); }
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.front(); }
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference top() const {
+ return c.front();
+ }
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI void push(const value_type& __v);
# ifndef _LIBCPP_CXX03_LANG
diff --git a/libcxx/test/libcxx/diagnostics/queue.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/queue.nodiscard.verify.cpp
index 77d3367cc2f4a..e94c314eaa1e1 100644
--- a/libcxx/test/libcxx/diagnostics/queue.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/queue.nodiscard.verify.cpp
@@ -12,12 +12,24 @@
#include <queue>
-void test_queue() {
- std::queue<int> queue;
- queue.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-}
+void test() {
+ {
+ std::queue<int> q;
+ const std::queue<int> cq{};
+
+ q.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ q.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ q.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cq.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ q.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cq.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ }
+
+ {
+ std::priority_queue<int> pq;
-void test_priority_queue() {
- std::priority_queue<int> priority_queue;
- priority_queue.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ pq.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ pq.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ pq.top(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ }
}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
[[nodiscard]][[nodiscard]]
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
d16e3e7 to
f6778c2
Compare
frederick-vs-ja
approved these changes
Nov 27, 2025
tanji-dg
pushed a commit
to tanji-dg/llvm-project
that referenced
this pull request
Nov 27, 2025
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
GeneraluseAI
pushed a commit
to GeneraluseAI/llvm-project
that referenced
this pull request
Nov 27, 2025
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
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.