Skip to content

Commit 89f6dbb

Browse files
committed
[libc++] Applied [[nodiscard]] to more Language Support classes
[[nodiscard]] should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html
1 parent 4125e73 commit 89f6dbb

File tree

6 files changed

+82
-25
lines changed

6 files changed

+82
-25
lines changed

libcxx/include/__new/exceptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {
3232
_LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
3333
_LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
3434
~bad_alloc() _NOEXCEPT override;
35-
const char* what() const _NOEXCEPT override;
35+
[[__nodiscard__]] const char* what() const _NOEXCEPT override;
3636
};
3737

3838
class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {
@@ -41,7 +41,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {
4141
_LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
4242
_LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
4343
~bad_array_new_length() _NOEXCEPT override;
44-
const char* what() const _NOEXCEPT override;
44+
[[__nodiscard__]] const char* what() const _NOEXCEPT override;
4545
};
4646

4747
#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME

libcxx/include/__new/new_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
2222
typedef void (*new_handler)();
2323
_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
24-
_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
24+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
2525
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
2626
#endif // _LIBCPP_ABI_VCRUNTIME
2727

libcxx/include/typeindex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ public:
8686
}
8787
# endif
8888

89-
_LIBCPP_HIDE_FROM_ABI size_t hash_code() const _NOEXCEPT { return __t_->hash_code(); }
90-
_LIBCPP_HIDE_FROM_ABI const char* name() const _NOEXCEPT { return __t_->name(); }
89+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t hash_code() const _NOEXCEPT { return __t_->hash_code(); }
90+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char* name() const _NOEXCEPT { return __t_->name(); }
9191
};
9292

9393
template <class _Tp>
9494
struct hash;
9595

9696
template <>
9797
struct hash<type_index> : public __unary_function<type_index, size_t> {
98-
_LIBCPP_HIDE_FROM_ABI size_t operator()(type_index __index) const _NOEXCEPT { return __index.hash_code(); }
98+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(type_index __index) const _NOEXCEPT {
99+
return __index.hash_code();
100+
}
99101
};
100102

101103
_LIBCPP_END_NAMESPACE_STD

libcxx/include/typeinfo

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ class _LIBCPP_EXPORTED_FROM_ABI type_info {
9595
public:
9696
virtual ~type_info();
9797

98-
const char* name() const _NOEXCEPT;
98+
[[__nodiscard__]] const char* name() const _NOEXCEPT;
9999

100-
_LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT { return __compare(__arg) < 0; }
100+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT {
101+
return __compare(__arg) < 0;
102+
}
101103

102-
size_t hash_code() const _NOEXCEPT;
104+
[[__nodiscard__]] size_t hash_code() const _NOEXCEPT;
103105

104106
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
105107
// When evaluated in a constant expression, both type infos simply can't come
@@ -336,7 +338,7 @@ public:
336338
_LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
337339
_LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
338340
~bad_cast() _NOEXCEPT override;
339-
const char* what() const _NOEXCEPT override;
341+
[[__nodiscard__]] const char* what() const _NOEXCEPT override;
340342
};
341343

342344
class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
@@ -345,7 +347,7 @@ public:
345347
_LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
346348
_LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
347349
~bad_typeid() _NOEXCEPT override;
348-
const char* what() const _NOEXCEPT override;
350+
[[__nodiscard__]] const char* what() const _NOEXCEPT override;
349351
};
350352

351353
} // namespace std

libcxx/test/libcxx/diagnostics/new.nodiscard.verify.cpp

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,54 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
9+
// <new>
1010

11-
// check that <array> functions are marked [[nodiscard]]
12-
13-
// clang-format off
11+
// Check that functions are marked [[nodiscard]]
1412

1513
#include <new>
1614

1715
#include "test_macros.h"
1816

1917
void test() {
20-
::operator new(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
21-
::operator new(0, std::nothrow); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22-
::operator new[](0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
23-
::operator new[](0, std::nothrow); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
18+
{
19+
std::bad_alloc ex;
20+
21+
ex.what(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
}
23+
{
24+
std::bad_array_new_length ex;
25+
26+
ex.what(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
27+
}
28+
29+
{
30+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
::operator new(0);
32+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
33+
::operator new(0, std::nothrow);
34+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
35+
::operator new[](0);
36+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
37+
::operator new[](0, std::nothrow);
2438
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
25-
::operator new(0, std::align_val_t{1}); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
26-
::operator new(0, std::align_val_t{1}, std::nothrow); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
27-
::operator new[](0, std::align_val_t{1}); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28-
::operator new[](0, std::align_val_t{1}, std::nothrow); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
40+
::operator new(0, std::align_val_t{1});
41+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
::operator new(0, std::align_val_t{1}, std::nothrow);
43+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
44+
::operator new[](0, std::align_val_t{1});
45+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
46+
::operator new[](0, std::align_val_t{1}, std::nothrow);
2947
#endif // _LIBCPP_HAS_ALIGNED_ALLOCATION
48+
}
49+
50+
std::get_new_handler(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3051

3152
#if TEST_STD_VER >= 17
32-
int* ptr = nullptr;
33-
std::launder(ptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
53+
{
54+
int* ptr = nullptr;
55+
56+
std::launder(ptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
57+
}
3458
#endif
3559
}

libcxx/test/libcxx/language.support/nodiscard.verify.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <coroutine>
1717
#include <exception>
1818
#include <initializer_list>
19+
#include <typeinfo>
20+
#include <typeindex>
1921

2022
#include "test_macros.h"
2123

@@ -126,4 +128,31 @@ void test() {
126128
il.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
127129
}
128130
#endif
131+
132+
{ // <typeindex>
133+
const std::type_index ti(typeid(int));
134+
135+
ti.hash_code(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
136+
ti.name(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
137+
138+
std::hash<std::type_index> hash;
139+
140+
hash(ti); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
141+
}
142+
143+
{ // <typeinfo>
144+
const std::type_info& ti = typeid(int);
145+
146+
ti.name(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
147+
ti.before(ti); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
148+
ti.hash_code(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
149+
150+
const std::bad_cast bc;
151+
152+
bc.what(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
153+
154+
const std::bad_typeid bt;
155+
156+
bc.what(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
157+
}
129158
}

0 commit comments

Comments
 (0)