Skip to content

Commit 7035de0

Browse files
committed
[libc++][hash] Applied [[nodiscard]]
`[[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 38678a9 commit 7035de0

File tree

7 files changed

+20
-5
lines changed

7 files changed

+20
-5
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/include/module.modulemap.in

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,10 @@ module std [system] {
16601660
}
16611661
module raw_storage_iterator { header "__memory/raw_storage_iterator.h" }
16621662
module shared_count { header "__memory/shared_count.h" }
1663-
module shared_ptr { header "__memory/shared_ptr.h" }
1663+
module shared_ptr {
1664+
header "__memory/shared_ptr.h"
1665+
export std.functional.hash
1666+
}
16641667
module swap_allocator { header "__memory/swap_allocator.h" }
16651668
module temp_value { header "__memory/temp_value.h" }
16661669
module temporary_buffer {
@@ -1673,6 +1676,7 @@ module std [system] {
16731676
}
16741677
module unique_ptr {
16751678
header "__memory/unique_ptr.h"
1679+
export std.functional.hash
16761680
}
16771681
module unique_temporary_buffer {
16781682
header "__memory/unique_temporary_buffer.h"
@@ -2391,6 +2395,7 @@ module std [system] {
23912395

23922396
header "coroutine"
23932397
export *
2398+
export std.functional.hash
23942399
}
23952400
} // module std
23962401

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/language.support/nodiscard.verify.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include <compare>
1414
#include <coroutine>
15-
#include <functional>
1615
#include <initializer_list>
1716

1817
#include "test_macros.h"

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)