Skip to content

Commit 2d8ffb9

Browse files
committed
[libc++][hash] Applied [[nodiscard]]
1 parent 38678a9 commit 2d8ffb9

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed

libcxx/include/__functional/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ struct hash : public __hash_impl<_Tp> {};
435435

436436
template <>
437437
struct hash<nullptr_t> : public __unary_function<nullptr_t, size_t> {
438-
_LIBCPP_HIDE_FROM_ABI size_t operator()(nullptr_t) const _NOEXCEPT { return 662607004ull; }
438+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(nullptr_t) const _NOEXCEPT { return 662607004ull; }
439439
};
440440

441441
#ifndef _LIBCPP_CXX03_LANG

libcxx/include/__memory/shared_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ struct hash<shared_ptr<_Tp> > {
14231423
_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
14241424
#endif
14251425

1426-
_LIBCPP_HIDE_FROM_ABI size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT {
1426+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const shared_ptr<_Tp>& __ptr) const _NOEXCEPT {
14271427
return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
14281428
}
14291429
};

libcxx/include/__memory/unique_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ struct hash<__enable_hash_helper< unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp,
800800
_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
801801
#endif
802802

803-
_LIBCPP_HIDE_FROM_ABI size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const {
803+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const {
804804
typedef typename unique_ptr<_Tp, _Dp>::pointer pointer;
805805
return hash<pointer>()(__ptr.get());
806806
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ void test() {
5959

6060
std::ref(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
6161
std::cref(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
62+
63+
// Hash specializations
64+
65+
std::hash<std::nullptr_t> hash;
66+
hash(nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
6267
}

libcxx/test/libcxx/utilities/smartptr/nodiscard.verify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ void test() {
4141
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
4242
std::make_unique_for_overwrite<int[]>(5);
4343
#endif
44+
45+
std::hash<std::unique_ptr<int>> hash;
46+
hash(uPtr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
4447
}
4548
{ // [util.sharedptr]
4649
std::shared_ptr<int[]> sPtr;
@@ -118,6 +121,9 @@ void test() {
118121
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
119122
std::get_deleter<int[]>(sPtr);
120123
#endif
124+
125+
std::hash<std::shared_ptr<int[]>> hash;
126+
hash(sPtr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
121127
}
122128
{ // [util.smartptr.weak]
123129
std::weak_ptr<int> wPtr;

0 commit comments

Comments
 (0)