[libcxx-commits] [libcxx] 5999cc8 - [libc++][stack] Applied `[[nodiscard]]` (#169468)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 25 08:55:39 PST 2025
Author: Hristo Hristov
Date: 2025-11-25T18:55:34+02:00
New Revision: 5999cc8ceef3acef128e1baf8fcefd7164acc677
URL: https://github.com/llvm/llvm-project/commit/5999cc8ceef3acef128e1baf8fcefd7164acc677
DIFF: https://github.com/llvm/llvm-project/commit/5999cc8ceef3acef128e1baf8fcefd7164acc677.diff
LOG: [libc++][stack] Applied `[[nodiscard]]` (#169468)
`[[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
Added:
Modified:
libcxx/include/stack
libcxx/test/libcxx/diagnostics/stack.nodiscard.verify.cpp
Removed:
################################################################################
diff --git a/libcxx/include/stack b/libcxx/include/stack
index a2f285c1994b9..537b82210b9d4 100644
--- a/libcxx/include/stack
+++ b/libcxx/include/stack
@@ -235,9 +235,9 @@ public:
# endif
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const { return c.empty(); }
- _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
- _LIBCPP_HIDE_FROM_ABI reference top() { return c.back(); }
- _LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.back(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference top() { return c.back(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.back(); }
_LIBCPP_HIDE_FROM_ABI void push(const value_type& __v) { c.push_back(__v); }
# ifndef _LIBCPP_CXX03_LANG
diff --git a/libcxx/test/libcxx/diagnostics/stack.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/stack.nodiscard.verify.cpp
index 861852ae07247..a0a5b3c898a8c 100644
--- a/libcxx/test/libcxx/diagnostics/stack.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/stack.nodiscard.verify.cpp
@@ -13,6 +13,11 @@
#include <stack>
void test() {
- std::stack<int> stack;
- stack.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::stack<int> st;
+ const std::stack<int> cst;
+
+ st.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st.top(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cst.top(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
More information about the libcxx-commits
mailing list