Skip to content

Commit 9031544

Browse files
authored
[libc++][bitset] Applied [[nodiscard]] (#170623)
`[[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/bitset
1 parent ba836dc commit 9031544

File tree

3 files changed

+95
-23
lines changed

3 files changed

+95
-23
lines changed

libcxx/include/__bit_reference

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public:
139139
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator bool() const _NOEXCEPT {
140140
return static_cast<bool>(*__seg_ & __mask_);
141141
}
142-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool operator~() const _NOEXCEPT {
142+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool operator~() const _NOEXCEPT {
143143
return !static_cast<bool>(*this);
144144
}
145145

libcxx/include/bitset

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -675,53 +675,62 @@ public:
675675
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& set(size_t __pos, bool __val = true);
676676
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& reset() _NOEXCEPT;
677677
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& reset(size_t __pos);
678-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator~() const _NOEXCEPT;
678+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator~() const _NOEXCEPT;
679679
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip() _NOEXCEPT;
680680
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip(size_t __pos);
681681

682682
// element access:
683683
# ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
684-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
684+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
685685
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
686686
return __base::__make_ref(__p);
687687
}
688688
# else
689-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference operator[](size_t __p) const {
689+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference operator[](size_t __p) const {
690690
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
691691
return __base::__make_ref(__p);
692692
}
693693
# endif
694-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {
694+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {
695695
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
696696
return __base::__make_ref(__p);
697697
}
698-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return __base::to_ulong(); }
699-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
698+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
699+
return __base::to_ulong();
700+
}
701+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
700702
return __base::to_ullong();
701703
}
702704
template <class _CharT, class _Traits, class _Allocator>
703-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
705+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
704706
to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
705707
template <class _CharT, class _Traits>
706-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, allocator<_CharT> >
708+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
709+
_LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, allocator<_CharT> >
707710
to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
708711
template <class _CharT>
709-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
712+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
713+
_LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
710714
to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
711-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<char, char_traits<char>, allocator<char> >
715+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
716+
_LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<char, char_traits<char>, allocator<char> >
712717
to_string(char __zero = '0', char __one = '1') const;
713-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t count() const _NOEXCEPT;
714-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT { return _Size; }
718+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t count() const _NOEXCEPT;
719+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT { return _Size; }
715720
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const bitset& __rhs) const _NOEXCEPT;
716721
# if _LIBCPP_STD_VER <= 17
717722
_LIBCPP_HIDE_FROM_ABI bool operator!=(const bitset& __rhs) const _NOEXCEPT;
718723
# endif
719-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool test(size_t __pos) const;
720-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return __base::all(); }
721-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return __base::any(); }
722-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT { return !any(); }
723-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator<<(size_t __pos) const _NOEXCEPT;
724-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator>>(size_t __pos) const _NOEXCEPT;
724+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool test(size_t __pos) const;
725+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT {
726+
return __base::all();
727+
}
728+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {
729+
return __base::any();
730+
}
731+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT { return !any(); }
732+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator<<(size_t __pos) const _NOEXCEPT;
733+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator>>(size_t __pos) const _NOEXCEPT;
725734

726735
private:
727736
template <class _CharT, class _Traits>
@@ -919,23 +928,23 @@ bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT {
919928
}
920929

921930
template <size_t _Size>
922-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
931+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
923932
operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
924933
bitset<_Size> __r = __x;
925934
__r &= __y;
926935
return __r;
927936
}
928937

929938
template <size_t _Size>
930-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
939+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
931940
operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
932941
bitset<_Size> __r = __x;
933942
__r |= __y;
934943
return __r;
935944
}
936945

937946
template <size_t _Size>
938-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
947+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
939948
operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
940949
bitset<_Size> __r = __x;
941950
__r ^= __y;
@@ -944,7 +953,9 @@ operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
944953

945954
template <size_t _Size>
946955
struct hash<bitset<_Size> > : public __unary_function<bitset<_Size>, size_t> {
947-
_LIBCPP_HIDE_FROM_ABI size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT { return __bs.__hash_code(); }
956+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT {
957+
return __bs.__hash_code();
958+
}
948959
};
949960

950961
template <class _CharT, class _Traits, size_t _Size>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// <bitset>
10+
11+
// Check that functions are marked [[nodiscard]]
12+
13+
#include <bitset>
14+
15+
#include "test_macros.h"
16+
#include "test_allocator.h"
17+
18+
void test() {
19+
std::bitset<11> bs;
20+
const std::bitset<11> cbs;
21+
22+
// std::bitset<>::reference operator~() const noexcept;
23+
~bs[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
24+
25+
~bs; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
26+
27+
bs[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
cbs[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
bs.to_ulong(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
bs.to_ullong(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
32+
struct CharTraits : public std::char_traits<char> {};
33+
34+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
35+
bs.to_string<char, CharTraits, test_allocator<char> >();
36+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
37+
bs.to_string<char, CharTraits>();
38+
#if !defined(TEST_HAS_NO_WIDE_CHARACTERS)
39+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
40+
bs.to_string<wchar_t>();
41+
#endif
42+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
43+
bs.to_string();
44+
45+
bs.count(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
46+
bs.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
47+
bs.test(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
48+
bs.all(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
49+
bs.any(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
50+
bs.none(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
51+
bs << 1; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
52+
bs >> 1; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
53+
54+
bs & bs; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
55+
bs | bs; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
56+
bs ^ bs; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
57+
58+
std::hash<std::bitset<11> > hash;
59+
60+
hash(bs); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
61+
}

0 commit comments

Comments
 (0)