Skip to content

Conversation

@H-G-Hristov
Copy link
Contributor

[[nodiscard]] should be applied to functions where discarding the return value is most likely a correctness issue.

`[[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
@H-G-Hristov H-G-Hristov requested a review from a team as a code owner November 25, 2025 07:43
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2025

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

Changes

[[nodiscard]] should be applied to functions where discarding the return value is most likely a correctness issue.


Full diff: https://github.com/llvm/llvm-project/pull/169468.diff

2 Files Affected:

  • (modified) libcxx/include/stack (+3-3)
  • (modified) libcxx/test/libcxx/diagnostics/stack.nodiscard.verify.cpp (+7-2)
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}}
 }

@Zingam
Copy link
Contributor

Zingam commented Nov 25, 2025

Thank you!

@Zingam Zingam merged commit 5999cc8 into llvm:main Nov 25, 2025
82 checks passed
@H-G-Hristov H-G-Hristov deleted the hgh/libcxx/nodiscard-to-stack branch November 25, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants