[libcxx-commits] [libcxx] [libc++] Add nodiscard attribute to std::make_unique/std::make_shared (PR #153115)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 13 16:19:59 PDT 2025


================
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+
+// check that functions are marked [[nodiscard]] as an extension in C++17
+
+// [[nodiscard]] std::make_unique(Args&&...);
+// [[nodiscard]] std::make_shared(Args&&...);
+//
+// [[nodiscard]] std::make_unique(size_t);
+// [[nodiscard]] std::make_shared(size_t);
+//
+// [[nodiscard]] std::make_unique_for_overwrite();
+// [[nodiscard]] std::make_shared_for_overwrite();
+
+#include <memory>
+
+void f() {
+
+    struct S {
+        S() = default;
+    };
+
+    std::make_unique<S>(S{}); // expected-warning {{ignoring return value of function}}
----------------
ldionne wrote:

These `.verify.cpp` tests use Clang-verify: https://clang.llvm.org/docs/InternalsManual.html#verifying-diagnostics

I think what @philnik777 is referring to is that you added `[[__nodiscard__]]` to 12 functions or function overloads, yet you are only testing that the diagnostic is firing on 6 functions. This means that you are missing coverage for 6 functions or function overloads to which the attribute was added, but you have nothing testing it.

https://github.com/llvm/llvm-project/pull/153115


More information about the libcxx-commits mailing list