Skip to content

Commit bf2987a

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 bf2987a

File tree

9 files changed

+49
-7
lines changed

9 files changed

+49
-7
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class _LIBCPP_EXPORTED_FROM_ABI bad_weak_ptr : public std::exception {
7777
_LIBCPP_HIDE_FROM_ABI bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
7878
_LIBCPP_HIDE_FROM_ABI bad_weak_ptr& operator=(const bad_weak_ptr&) _NOEXCEPT = default;
7979
~bad_weak_ptr() _NOEXCEPT override;
80-
const char* what() const _NOEXCEPT override;
80+
[[__nodiscard__]] const char* what() const _NOEXCEPT override;
8181
};
8282

8383
[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_weak_ptr() {
@@ -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/__utility/integer_sequence.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ template <class _Tp, _Tp... _Indices>
3333
struct __integer_sequence {
3434
using value_type = _Tp;
3535
static_assert(is_integral<_Tp>::value, "std::integer_sequence can only be instantiated with an integral type");
36-
static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Indices); }
36+
[[__nodiscard__]] static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Indices); }
3737
};
3838

3939
template <size_t... _Indices>

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// check that <functional> functions are marked [[nodiscard]]
1212

13+
#include <cstddef>
1314
#include <functional>
1415

1516
#include "test_macros.h"
@@ -59,4 +60,9 @@ void test() {
5960

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

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"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// UNSUPPORTED: c++03
10+
11+
// <utility>
12+
13+
// Check that functions are marked [[nodiscard]]
14+
15+
#include <utility>
16+
17+
void test() {
18+
std::integer_sequence<int, 49, 82, 94> seq;
19+
20+
seq.size(); // expected-warning@ {{ignoring return value of function declared with 'nodiscard' attribute}}
21+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ 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}}
47+
}
48+
{ // [util.smartptr.weak.bad]
49+
std::bad_weak_ptr bwp;
50+
51+
bwp.what(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
4452
}
4553
{ // [util.sharedptr]
4654
std::shared_ptr<int[]> sPtr;
@@ -118,6 +126,9 @@ void test() {
118126
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
119127
std::get_deleter<int[]>(sPtr);
120128
#endif
129+
130+
std::hash<std::shared_ptr<int[]>> hash;
131+
hash(sPtr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
121132
}
122133
{ // [util.smartptr.weak]
123134
std::weak_ptr<int> wPtr;

0 commit comments

Comments
 (0)