Skip to content

Commit 5999cc8

Browse files
authored
[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
1 parent d69e701 commit 5999cc8

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

libcxx/include/stack

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ public:
235235
# endif
236236

237237
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const { return c.empty(); }
238-
_LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
239-
_LIBCPP_HIDE_FROM_ABI reference top() { return c.back(); }
240-
_LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.back(); }
238+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
239+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference top() { return c.back(); }
240+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.back(); }
241241

242242
_LIBCPP_HIDE_FROM_ABI void push(const value_type& __v) { c.push_back(__v); }
243243
# ifndef _LIBCPP_CXX03_LANG

libcxx/test/libcxx/diagnostics/stack.nodiscard.verify.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
#include <stack>
1414

1515
void test() {
16-
std::stack<int> stack;
17-
stack.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
16+
std::stack<int> st;
17+
const std::stack<int> cst;
18+
19+
st.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
20+
st.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
21+
st.top(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
cst.top(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
1823
}

0 commit comments

Comments
 (0)