Skip to content

Commit 4125e73

Browse files
H-G-Hristovfrederick-vs-jaZingam
authored
[libc++] Applied [[nodiscard]] to hash<shared_ptr>, hash<unique_ptr>, etc. (#170674)
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html 1. `hash<shared_ptr>`, `hash<unique_ptr>`, `std::integer_sequence<>` etc. 2. Also implements fixes to #169634 on the go (issues discovered during current implementation) --------- Co-authored-by: A. Jiang <[email protected]> Co-authored-by: Hristo Hristov <[email protected]>
1 parent e442904 commit 4125e73

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
@@ -1661,7 +1661,10 @@ module std [system] {
16611661
}
16621662
module raw_storage_iterator { header "__memory/raw_storage_iterator.h" }
16631663
module shared_count { header "__memory/shared_count.h" }
1664-
module shared_ptr { header "__memory/shared_ptr.h" }
1664+
module shared_ptr {
1665+
header "__memory/shared_ptr.h"
1666+
export std.functional.hash
1667+
}
16651668
module swap_allocator { header "__memory/swap_allocator.h" }
16661669
module temp_value { header "__memory/temp_value.h" }
16671670
module temporary_buffer {
@@ -1674,6 +1677,7 @@ module std [system] {
16741677
}
16751678
module unique_ptr {
16761679
header "__memory/unique_ptr.h"
1680+
export std.functional.hash
16771681
}
16781682
module unique_temporary_buffer {
16791683
header "__memory/unique_temporary_buffer.h"
@@ -2392,6 +2396,7 @@ module std [system] {
23922396

23932397
header "coroutine"
23942398
export *
2399+
export std.functional.hash
23952400
}
23962401
} // module std
23972402

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
@@ -15,7 +15,6 @@
1515
#include <compare>
1616
#include <coroutine>
1717
#include <exception>
18-
#include <functional>
1918
#include <initializer_list>
2019

2120
#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+
// REQUIRES: std-at-least-c++14
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)