Skip to content

Commit 1042cef

Browse files
committed
[libc++] Added [[nodiscard]] to fstream.native_handle, span.at and in/out_ptr
...according to Coding Guidelines: `[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. At the time of impelentation the ``nodiscard` policy has not been established yet. I did it as a batch as the changes are trivial and minimal.
1 parent 77b9301 commit 1042cef

File tree

7 files changed

+130
-12
lines changed

7 files changed

+130
-12
lines changed

libcxx/include/__memory/inout_ptr.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ class inout_ptr_t {
7979
}
8080
}
8181

82-
_LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
82+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept {
83+
return std::addressof(const_cast<_Pointer&>(__p_));
84+
}
8385

84-
_LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
86+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
8587
requires(!is_same_v<_Pointer, void*>)
8688
{
8789
static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
@@ -96,7 +98,7 @@ class inout_ptr_t {
9698
};
9799

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

libcxx/include/__memory/out_ptr.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ class out_ptr_t {
7171
}
7272
}
7373

74-
_LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
74+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept {
75+
return std::addressof(const_cast<_Pointer&>(__p_));
76+
}
7577

76-
_LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
78+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
7779
requires(!is_same_v<_Pointer, void*>)
7880
{
7981
static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
@@ -88,7 +90,7 @@ class out_ptr_t {
8890
};
8991

9092
template <class _Pointer = void, class _Smart, class... _Args>
91-
_LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {
93+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {
9294
using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
9395
return std::out_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
9496
}

libcxx/include/fstream

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public:
269269
_LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
270270
basic_filebuf* close();
271271
# if _LIBCPP_STD_VER >= 26
272-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
272+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
273273
_LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
274274
# if defined(_LIBCPP_WIN32API)
275275
return std::__filebuf_windows_native_handle(__file_);
@@ -1165,7 +1165,7 @@ public:
11651165

11661166
_LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
11671167
# if _LIBCPP_STD_VER >= 26
1168-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1168+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
11691169
# endif
11701170
_LIBCPP_HIDE_FROM_ABI bool is_open() const;
11711171
void open(const char* __s, ios_base::openmode __mode = ios_base::in);
@@ -1321,7 +1321,7 @@ public:
13211321

13221322
_LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
13231323
# if _LIBCPP_STD_VER >= 26
1324-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1324+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
13251325
# endif
13261326
_LIBCPP_HIDE_FROM_ABI bool is_open() const;
13271327
void open(const char* __s, ios_base::openmode __mode = ios_base::out);
@@ -1483,7 +1483,7 @@ public:
14831483

14841484
_LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
14851485
# if _LIBCPP_STD_VER >= 26
1486-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1486+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
14871487
# endif
14881488
_LIBCPP_HIDE_FROM_ABI bool is_open() const;
14891489
_LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);

libcxx/include/span

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public:
362362
}
363363

364364
# if _LIBCPP_STD_VER >= 26
365-
_LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
365+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
366366
if (__index >= size())
367367
std::__throw_out_of_range("span");
368368
return __data_[__index];
@@ -527,7 +527,7 @@ public:
527527
}
528528

529529
# if _LIBCPP_STD_VER >= 26
530-
_LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
530+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
531531
if (__index >= size())
532532
std::__throw_out_of_range("span");
533533
return __data_[__index];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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++26
10+
11+
// <span>
12+
13+
// Check that functions are marked [[nodiscard]]
14+
15+
#include <array>
16+
#include <span>
17+
#include <vector>
18+
19+
void test() {
20+
{ // Test with static extent
21+
std::array arr{94, 92};
22+
std::span sp{arr};
23+
sp.at(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
24+
}
25+
{ // Test with dynamic extent
26+
std::vector vec{94, 92};
27+
std::span sp{vec};
28+
sp.at(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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++26
10+
11+
// <fstream>
12+
13+
// Check that functions are marked [[nodiscard]]
14+
15+
#include <fstream>
16+
17+
void test() {
18+
{
19+
std::filebuf fb;
20+
fb.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
21+
}
22+
{
23+
std::fstream fs;
24+
fs.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
25+
}
26+
{
27+
std::ifstream fs;
28+
fs.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
}
30+
{
31+
std::ofstream fs;
32+
fs.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
33+
}
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
void test() {
18+
// clang-format off
19+
std::unique_ptr<int> uPtr;
20+
21+
// [inout.ptr]
22+
{
23+
{ // Test inout_ptr()
24+
std::inout_ptr(uPtr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
25+
}
26+
{ // Test operator Pointer*()
27+
const std::inout_ptr_t<std::unique_ptr<int>, int*> iPtr{uPtr};
28+
iPtr.operator int**(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
}
30+
{ // Test operator void**()
31+
const std::inout_ptr_t<std::unique_ptr<int>, void*> iPtr{uPtr};
32+
iPtr.operator void**(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
33+
}
34+
}
35+
// [out.ptr]
36+
{
37+
{ // Test out_ptr()
38+
std::out_ptr(uPtr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
}
40+
{ // Test operator Pointer*()
41+
const std::out_ptr_t<std::unique_ptr<int>, int*> oPtr{uPtr};
42+
oPtr.operator int**(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
43+
}
44+
{ // Test operator void**()
45+
const std::out_ptr_t<std::unique_ptr<int>, void*> oPtr{uPtr};
46+
oPtr.operator void**(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
47+
}
48+
}
49+
// clang-format on
50+
}

0 commit comments

Comments
 (0)