From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 9397C3858417; Wed, 28 Feb 2024 11:29:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9397C3858417 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709119792; bh=G64f/QvSKkd7ocNZDRUWj1Qz/yIPsFIY0/9eU3xoCJM=; h=From:To:Subject:Date:From; b=h9XkO3FEH8aMQdJ/3NE3VGWiUIsiFDXhvvUOpWK56YLNgUadp9eM2WWR4JB3i2cKZ uz19LDX5dvt8Z4SR7IsL89jGBaBKl4QRJrnxqPMk5AaS/aRWptr8dvHKLLMQmLZGHV /YfAa0iTfZ5KLLfWuP9TtH2bVlrZ3W/ULeuTEvgA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-9214] libstdc++: Add more nodiscard uses in X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: cf918498ab7c717e3c6badab44340117af53ec35 X-Git-Newrev: 2d8cd712b17e88f2935237665799d0c72b7ce1b6 Message-Id: <20240228112952.9397C3858417@sourceware.org> Date: Wed, 28 Feb 2024 11:29:52 +0000 (GMT) List-Id: https://gcc.gnu.org/g:2d8cd712b17e88f2935237665799d0c72b7ce1b6 commit r14-9214-g2d8cd712b17e88f2935237665799d0c72b7ce1b6 Author: Jonathan Wakely Date: Mon Feb 26 13:09:02 2024 +0000 libstdc++: Add more nodiscard uses in Add [[nodiscard]] to vector::at and to comparison operators. libstdc++-v3/ChangeLog: * include/bits/stl_bvector.h (vector::at): Add nodiscard. * include/bits/stl_vector.h (vector::at): Likewise. (operator==, operator<=>, operator<, operator!=, operator>) (operator<=, operator>=): Likewise. * include/debug/vector (operator==, operator<=>, operator<) (operator!=, operator>, operator<=, operator>=): Likewise. * testsuite/23_containers/vector/nodiscard.cc: New test. Diff: --- libstdc++-v3/include/bits/stl_bvector.h | 4 +- libstdc++-v3/include/bits/stl_vector.h | 18 +-- libstdc++-v3/include/debug/vector | 8 +- .../testsuite/23_containers/vector/nodiscard.cc | 153 +++++++++++++++++++++ 4 files changed, 171 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h index aa5644b4a0e..2c8b892b07a 100644 --- a/libstdc++-v3/include/bits/stl_bvector.h +++ b/libstdc++-v3/include/bits/stl_bvector.h @@ -1101,7 +1101,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER } public: - _GLIBCXX20_CONSTEXPR + _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR reference at(size_type __n) { @@ -1109,7 +1109,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER return (*this)[__n]; } - _GLIBCXX20_CONSTEXPR + _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR const_reference at(size_type __n) const { diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index 6a9543eefce..a8d387f40a1 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -1172,7 +1172,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * is first checked that it is in the range of the vector. The * function throws out_of_range if the check fails. */ - _GLIBCXX20_CONSTEXPR + _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR reference at(size_type __n) { @@ -1191,7 +1191,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * is first checked that it is in the range of the vector. The * function throws out_of_range if the check fails. */ - _GLIBCXX20_CONSTEXPR + _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR const_reference at(size_type __n) const { @@ -2042,7 +2042,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * and if corresponding elements compare equal. */ template - _GLIBCXX20_CONSTEXPR + _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR inline bool operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return (__x.size() == __y.size() @@ -2061,7 +2061,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * `<` and `>=` etc. */ template - _GLIBCXX20_CONSTEXPR + [[nodiscard]] _GLIBCXX20_CONSTEXPR inline __detail::__synth3way_t<_Tp> operator<=>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { @@ -2082,32 +2082,32 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER * See std::lexicographical_compare() for how the determination is made. */ template - inline bool + _GLIBCXX_NODISCARD inline bool operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); } /// Based on operator== template - inline bool + _GLIBCXX_NODISCARD inline bool operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return !(__x == __y); } /// Based on operator< template - inline bool + _GLIBCXX_NODISCARD inline bool operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return __y < __x; } /// Based on operator< template - inline bool + _GLIBCXX_NODISCARD inline bool operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return !(__y < __x); } /// Based on operator< template - inline bool + _GLIBCXX_NODISCARD inline bool operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return !(__x < __y); } #endif // three-way comparison diff --git a/libstdc++-v3/include/debug/vector b/libstdc++-v3/include/debug/vector index 5a0fc808651..216822975a2 100644 --- a/libstdc++-v3/include/debug/vector +++ b/libstdc++-v3/include/debug/vector @@ -866,7 +866,7 @@ namespace __debug }; template - _GLIBCXX20_CONSTEXPR + _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR inline bool operator==(const vector<_Tp, _Alloc>& __lhs, const vector<_Tp, _Alloc>& __rhs) @@ -874,35 +874,41 @@ namespace __debug #if __cpp_lib_three_way_comparison template + [[nodiscard]] constexpr __detail::__synth3way_t<_Tp> operator<=>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return __x._M_base() <=> __y._M_base(); } #else template + _GLIBCXX_NODISCARD inline bool operator!=(const vector<_Tp, _Alloc>& __lhs, const vector<_Tp, _Alloc>& __rhs) { return __lhs._M_base() != __rhs._M_base(); } template + _GLIBCXX_NODISCARD inline bool operator<(const vector<_Tp, _Alloc>& __lhs, const vector<_Tp, _Alloc>& __rhs) { return __lhs._M_base() < __rhs._M_base(); } template + _GLIBCXX_NODISCARD inline bool operator<=(const vector<_Tp, _Alloc>& __lhs, const vector<_Tp, _Alloc>& __rhs) { return __lhs._M_base() <= __rhs._M_base(); } template + _GLIBCXX_NODISCARD inline bool operator>=(const vector<_Tp, _Alloc>& __lhs, const vector<_Tp, _Alloc>& __rhs) { return __lhs._M_base() >= __rhs._M_base(); } template + _GLIBCXX_NODISCARD inline bool operator>(const vector<_Tp, _Alloc>& __lhs, const vector<_Tp, _Alloc>& __rhs) diff --git a/libstdc++-v3/testsuite/23_containers/vector/nodiscard.cc b/libstdc++-v3/testsuite/23_containers/vector/nodiscard.cc new file mode 100644 index 00000000000..3b5480d16d4 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/nodiscard.cc @@ -0,0 +1,153 @@ +// { dg-do compile { target c++17 } } + +#include + +void +test_observers(std::vector v) +{ + v.size(); // { dg-warning "ignoring return value" } + v.capacity(); // { dg-warning "ignoring return value" } + v.empty(); // { dg-warning "ignoring return value" } +} + +void +test_element_access(std::vector v) +{ + v.front(); // { dg-warning "ignoring return value" } + v.back(); // { dg-warning "ignoring return value" } + v[1]; // { dg-warning "ignoring return value" } + v.at(1); // { dg-warning "ignoring return value" } + v.data(); // { dg-warning "ignoring return value" } + const auto& cv = v; + cv[1]; // { dg-warning "ignoring return value" } + cv.at(1); // { dg-warning "ignoring return value" } + cv.data(); // { dg-warning "ignoring return value" } +} + +void +test_rel_ops(std::vector v) +{ + v == v; // { dg-warning "ignoring return value" } + v != v; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + v < v; // { dg-warning "ignoring return value" } + v > v; // { dg-warning "ignoring return value" } + v <= v; // { dg-warning "ignoring return value" } + v >= v; // { dg-warning "ignoring return value" } +} + +struct S { }; + +void +test_iterators(std::vector v) +{ + v.begin(); // { dg-warning "ignoring return value" } + v.end(); // { dg-warning "ignoring return value" } + v.rbegin(); // { dg-warning "ignoring return value" } + v.rend(); // { dg-warning "ignoring return value" } + const auto& cv = v; + cv.begin(); // { dg-warning "ignoring return value" } + cv.end(); // { dg-warning "ignoring return value" } + cv.rbegin(); // { dg-warning "ignoring return value" } + cv.rend(); // { dg-warning "ignoring return value" } + + v.cbegin(); // { dg-warning "ignoring return value" } + v.cend(); // { dg-warning "ignoring return value" } + v.crbegin(); // { dg-warning "ignoring return value" } + v.crend(); // { dg-warning "ignoring return value" } + + auto i = v.begin(), j = v.end(); + i == j; // { dg-warning "ignoring return value" } + i != j; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + i < j; // { dg-warning "ignoring return value" } + i > j; // { dg-warning "ignoring return value" } + i <= j; // { dg-warning "ignoring return value" } + i >= j; // { dg-warning "ignoring return value" } + + auto ci = cv.begin(), cj = cv.end(); + ci == cj; // { dg-warning "ignoring return value" } + ci != cj; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + ci < cj; // { dg-warning "ignoring return value" } + ci > cj; // { dg-warning "ignoring return value" } + ci <= cj; // { dg-warning "ignoring return value" } + ci >= cj; // { dg-warning "ignoring return value" } + + ci == j; // { dg-warning "ignoring return value" } + ci != j; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + ci < j; // { dg-warning "ignoring return value" } + ci > j; // { dg-warning "ignoring return value" } + ci <= j; // { dg-warning "ignoring return value" } + ci >= j; // { dg-warning "ignoring return value" } +} + +void +test_observers(std::vector v) +{ + v.size(); // { dg-warning "ignoring return value" } + v.capacity(); // { dg-warning "ignoring return value" } + v.empty(); // { dg-warning "ignoring return value" } +} + +void +test_element_access(std::vector v) +{ + v.front(); // { dg-warning "ignoring return value" } + v.back(); // { dg-warning "ignoring return value" } + v[1]; // { dg-warning "ignoring return value" } + v.at(1); // { dg-warning "ignoring return value" } + const auto& cv = v; + cv[1]; // { dg-warning "ignoring return value" } + cv.at(1); // { dg-warning "ignoring return value" } +} + +void +test_rel_ops(std::vector v) +{ + v == v; // { dg-warning "ignoring return value" } + v != v; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + v < v; // { dg-warning "ignoring return value" } + v > v; // { dg-warning "ignoring return value" } + v <= v; // { dg-warning "ignoring return value" } + v >= v; // { dg-warning "ignoring return value" } +} + +void +test_iterators(std::vector v) +{ + v.begin(); // { dg-warning "ignoring return value" } + v.end(); // { dg-warning "ignoring return value" } + v.rbegin(); // { dg-warning "ignoring return value" } + v.rend(); // { dg-warning "ignoring return value" } + const auto& cv = v; + cv.begin(); // { dg-warning "ignoring return value" } + cv.end(); // { dg-warning "ignoring return value" } + cv.rbegin(); // { dg-warning "ignoring return value" } + cv.rend(); // { dg-warning "ignoring return value" } + + v.cbegin(); // { dg-warning "ignoring return value" } + v.cend(); // { dg-warning "ignoring return value" } + v.crbegin(); // { dg-warning "ignoring return value" } + v.crend(); // { dg-warning "ignoring return value" } + + auto i = v.begin(), j = v.end(); + i == j; // { dg-warning "ignoring return value" } + i != j; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + i < j; // { dg-warning "ignoring return value" } + i > j; // { dg-warning "ignoring return value" } + i <= j; // { dg-warning "ignoring return value" } + i >= j; // { dg-warning "ignoring return value" } + + auto ci = cv.begin(), cj = cv.end(); + ci == cj; // { dg-warning "ignoring return value" } + ci != cj; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + ci < cj; // { dg-warning "ignoring return value" } + ci > cj; // { dg-warning "ignoring return value" } + ci <= cj; // { dg-warning "ignoring return value" } + ci >= cj; // { dg-warning "ignoring return value" } + + ci == j; // { dg-warning "ignoring return value" } + ci != j; // { dg-warning "ignoring return value" "PR c++/114104" { target c++17_down } } + ci < j; // { dg-warning "ignoring return value" } + ci > j; // { dg-warning "ignoring return value" } + ci <= j; // { dg-warning "ignoring return value" } + ci >= j; // { dg-warning "ignoring return value" } +}