Skip to content

Commit 9036e23

Browse files
H-G-HristovZingam
andauthored
[libc++] Apply [[nodiscard]] to in/out_ptr (#167097)
...according to Coding Guidelines: `[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. Changes to: - [x] `inout_ptr()` - [x] `out_ptr()` At the time of impelentation the `[[nodiscard]]` policy has not been established yet. --------- Co-authored-by: Hristo Hristov <[email protected]>
1 parent 5305a53 commit 9036e23

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

libcxx/include/__memory/inout_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class inout_ptr_t {
9696
};
9797

9898
template <class _Pointer = void, class _Smart, class... _Args>
99-
_LIBCPP_HIDE_FROM_ABI auto inout_ptr(_Smart& __s, _Args&&... __args) {
99+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI auto inout_ptr(_Smart& __s, _Args&&... __args) {
100100
using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
101101
return std::inout_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
102102
}

libcxx/include/__memory/out_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class out_ptr_t {
8888
};
8989

9090
template <class _Pointer = void, class _Smart, class... _Args>
91-
_LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {
91+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {
9292
using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
9393
return std::out_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
9494
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++23
10+
11+
// <memory>
12+
13+
// Check that functions are marked [[nodiscard]]
14+
15+
#include <memory>
16+
17+
#include "test_macros.h"
18+
19+
void test() {
20+
#if TEST_STD_VER >= 23
21+
{
22+
std::unique_ptr<int> uPtr;
23+
// [inout.ptr]
24+
std::inout_ptr(uPtr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
25+
// [out.ptr]
26+
std::out_ptr(uPtr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
27+
}
28+
#endif
29+
}

0 commit comments

Comments
 (0)