[libcxx-commits] [libcxx] [libc++][memory] Applied `[[nodiscard]]` to smart pointers (PR #168483)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 18 08:18:06 PST 2025
================
@@ -6,19 +6,147 @@
//
//===----------------------------------------------------------------------===//
-// REQUIRES: std-at-least-c++23
-
// <memory>
// Check that functions are marked [[nodiscard]]
#include <memory>
+#include <utility>
#include "test_macros.h"
void test() {
+ { // [unique.ptr]
+ std::unique_ptr<int> uPtr;
+
+ *uPtr; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ uPtr.get(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ uPtr.get_deleter(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ const std::unique_ptr<int> cuPtr;
+ cuPtr.get_deleter(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+#if TEST_STD_VER >= 14
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_unique<int>(94);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_unique<int[]>(82);
+#endif
+#if TEST_STD_VER >= 20
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_unique_for_overwrite<int>();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_unique_for_overwrite<int[]>(5);
+#endif
+ }
+ { // [util.sharedptr]
+ std::shared_ptr<int[]> sPtr;
+
+ sPtr.get(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ *sPtr; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ sPtr.use_count(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ sPtr.owner_before(std::shared_ptr<int>());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ sPtr.owner_before(std::weak_ptr<int>());
+#if TEST_STD_VER >= 17
+ sPtr[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared<int>(std::allocator<int>(), 5);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared<int>();
+#if TEST_STD_VER >= 20
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared_for_overwrite<int>(std::allocator<int>());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared_for_overwrite<int>();
+
+ // Bounded array variants
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared<int[5]>(std::allocator<int>());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared<int[5]>(std::allocator<int>(), 5);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared_for_overwrite<int[5]>(std::allocator<int>());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared<int[5]>();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared<int[5]>(94);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared_for_overwrite<int[5]>();
+
+ // Unbounded array variants
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared<int[]>(std::allocator<int>(), 5);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared<int[]>(std::allocator<int>(), 5, 94);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::allocate_shared_for_overwrite<int[]>(std::allocator<int>(), 5);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared<int[]>(5);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared<int[]>(5, 82);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::make_shared_for_overwrite<int[]>(5);
+#endif
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::static_pointer_cast<int[]>(sPtr);
+#if TEST_STD_VER >= 20
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::static_pointer_cast<int[]>(std::move(sPtr));
+#endif
+ class Empty {};
+ std::shared_ptr<Empty> dsPtr;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::dynamic_pointer_cast<Empty>(dsPtr);
+#if TEST_STD_VER >= 20
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::dynamic_pointer_cast<Empty>(std::move(dsPtr));
+#endif
----------------
philnik777 wrote:
I don't think we need to guard this. It'll simply use the `const shared_ptr&` overload if the `shared_ptr&&` one isn't available. Throughout.
https://github.com/llvm/llvm-project/pull/168483
More information about the libcxx-commits
mailing list