Skip to content

Commit 100d36f

Browse files
committed
[libc++][bitset] 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/bitset
1 parent 38678a9 commit 100d36f

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

libcxx/include/bitset

Lines changed: 27 additions & 18 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>
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+
// UNSUPPORTED: c++03
10+
11+
// <bitset>
12+
13+
// Check that functions are marked [[nodiscard]]
14+
15+
#include <bitset>
16+
17+
#include "test_macros.h"
18+
#include "test_allocator.h"
19+
20+
void test() {
21+
std::bitset<10> bs;
22+
const std::bitset<10> cbs;
23+
24+
~bs; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
25+
26+
bs[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
27+
cbs[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
bs.to_ulong(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
bs.to_ullong(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
31+
struct CharTraits : public std::char_traits<char> {};
32+
33+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
34+
bs.to_string<char, CharTraits, test_allocator<char>>();
35+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
36+
bs.to_string<char, CharTraits>();
37+
#if !defined(TEST_HAS_NO_WIDE_CHARACTERS)
38+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
bs.to_string<wchar_t>();
40+
#endif
41+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
bs.to_string();
43+
44+
bs.count(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
45+
bs.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
46+
bs.test(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
47+
bs.all(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
48+
bs.any(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
49+
bs.none(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
50+
bs << 1; // 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+
}

0 commit comments

Comments
 (0)