[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
This commit is contained in:
Hristo Hristov
2025-11-25 18:55:34 +02:00
committed by GitHub
parent d69e701496
commit 5999cc8cee
2 changed files with 10 additions and 5 deletions

View File

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

View File

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