Skip to content

Commit 4ef24c0

Browse files
committed
[libc++][pair] 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 - https://wg21.link/pairs
1 parent 80ec43d commit 4ef24c0

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

libcxx/include/__utility/pair.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ swap(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) noexcept(noexcept(__x
539539
#endif
540540

541541
template <class _T1, class _T2>
542-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<__unwrap_ref_decay_t<_T1>, __unwrap_ref_decay_t<_T2> >
543-
make_pair(_T1&& __t1, _T2&& __t2) {
542+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
543+
pair<__unwrap_ref_decay_t<_T1>, __unwrap_ref_decay_t<_T2> > make_pair(_T1&& __t1, _T2&& __t2) {
544544
return pair<__unwrap_ref_decay_t<_T1>, __unwrap_ref_decay_t<_T2> >(std::forward<_T1>(__t1), std::forward<_T2>(__t2));
545545
}
546546

@@ -612,67 +612,71 @@ struct __get_pair<1> {
612612
};
613613

614614
template <size_t _Ip, class _T1, class _T2>
615-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Ip, pair<_T1, _T2> >::type&
615+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
616+
typename tuple_element<_Ip, pair<_T1, _T2> >::type&
616617
get(pair<_T1, _T2>& __p) _NOEXCEPT {
617618
return __get_pair<_Ip>::get(__p);
618619
}
619620

620621
template <size_t _Ip, class _T1, class _T2>
621-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
622+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
623+
_LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
622624
get(const pair<_T1, _T2>& __p) _NOEXCEPT {
623625
return __get_pair<_Ip>::get(__p);
624626
}
625627

626628
template <size_t _Ip, class _T1, class _T2>
627-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
629+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
630+
typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
628631
get(pair<_T1, _T2>&& __p) _NOEXCEPT {
629632
return __get_pair<_Ip>::get(std::move(__p));
630633
}
631634

632635
template <size_t _Ip, class _T1, class _T2>
633-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
636+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
637+
_LIBCPP_CONSTEXPR_SINCE_CXX14 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
634638
get(const pair<_T1, _T2>&& __p) _NOEXCEPT {
635639
return __get_pair<_Ip>::get(std::move(__p));
636640
}
637641

638642
#if _LIBCPP_STD_VER >= 14
639643
template <class _T1, class _T2>
640-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1& get(pair<_T1, _T2>& __p) _NOEXCEPT {
644+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T1& get(pair<_T1, _T2>& __p) _NOEXCEPT {
641645
return __p.first;
642646
}
643647

644648
template <class _T1, class _T2>
645-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const& get(pair<_T1, _T2> const& __p) _NOEXCEPT {
649+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const& get(pair<_T1, _T2> const& __p) _NOEXCEPT {
646650
return __p.first;
647651
}
648652

649653
template <class _T1, class _T2>
650-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
654+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T1&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
651655
return std::forward<_T1&&>(__p.first);
652656
}
653657

654658
template <class _T1, class _T2>
655-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const&& get(pair<_T1, _T2> const&& __p) _NOEXCEPT {
659+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T1 const&& get(pair<_T1, _T2> const&& __p) _NOEXCEPT {
656660
return std::forward<_T1 const&&>(__p.first);
657661
}
658662

659663
template <class _T2, class _T1>
660-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T2& get(pair<_T1, _T2>& __p) _NOEXCEPT {
664+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T2& get(pair<_T1, _T2>& __p) _NOEXCEPT {
661665
return __p.second;
662666
}
663667

664668
template <class _T2, class _T1>
665-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T2 const& get(pair<_T1, _T2> const& __p) _NOEXCEPT {
669+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T2 const& get(pair<_T1, _T2> const& __p) _NOEXCEPT {
666670
return __p.second;
667671
}
668672

669673
template <class _T2, class _T1>
670-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T2&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
674+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T2&& get(pair<_T1, _T2>&& __p) _NOEXCEPT {
671675
return std::forward<_T2&&>(__p.second);
672676
}
673677

674678
template <class _T2, class _T1>
675-
inline _LIBCPP_HIDE_FROM_ABI constexpr _T2 const&& get(pair<_T1, _T2> const&& __p) _NOEXCEPT {
679+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI constexpr _T2 const&& get(pair<_T1, _T2> const&& __p) _NOEXCEPT {
676680
return std::forward<_T2 const&&>(__p.second);
677681
}
678682

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
// Check that functions are marked [[nodiscard]]
10+
11+
#include <utility>
12+
13+
#include <test_macros.h>
14+
15+
void test() {
16+
struct First {};
17+
struct Second {};
18+
19+
std::pair<First, Second> p;
20+
const std::pair<First, Second> cp{};
21+
22+
std::make_pair(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
23+
24+
#if TEST_STD_VER >= 11
25+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
26+
std::get<0>(p);
27+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
std::get<0>(cp);
29+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
std::get<0>(std::move(p));
31+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
32+
std::get<0>(std::move(cp));
33+
#endif // TEST_STD_VER >= 11
34+
#if TEST_STD_VER >= 14
35+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
36+
std::get<First>(p);
37+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
38+
std::get<First>(cp);
39+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
40+
std::get<First>(std::move(p));
41+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
std::get<First>(std::move(cp));
43+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
44+
std::get<Second>(p);
45+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
46+
std::get<Second>(cp);
47+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
48+
std::get<Second>(std::move(p));
49+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
50+
std::get<Second>(std::move(cp));
51+
#endif // TEST_STD_VER >= 14
52+
}

0 commit comments

Comments
 (0)