]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add more nodiscard uses in <vector>
authorJonathan Wakely <jwakely@redhat.com>
Mon, 26 Feb 2024 13:09:02 +0000 (13:09 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 28 Feb 2024 11:27:46 +0000 (11:27 +0000)
Add [[nodiscard]] to vector::at and to comparison operators.

libstdc++-v3/ChangeLog:

* include/bits/stl_bvector.h (vector<bool, A>::at): Add
nodiscard.
* include/bits/stl_vector.h (vector<T, A>::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.

libstdc++-v3/include/bits/stl_bvector.h
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/include/debug/vector
libstdc++-v3/testsuite/23_containers/vector/nodiscard.cc [new file with mode: 0644]

index aa5644b4a0e1285030c519cfb2fd118947492a73..2c8b892b07a43f886105fafa9dda3956314336e0 100644 (file)
@@ -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
       {
index 6a9543eefce8cdbcceae02c1ac1b87965a46b03b..a8d387f40a1990896a8933c6c132c0e731fd3f32 100644 (file)
@@ -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<typename _Tp, typename _Alloc>
-    _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<typename _Tp, typename _Alloc>
-    _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<typename _Tp, typename _Alloc>
-    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<typename _Tp, typename _Alloc>
-    inline bool
+    _GLIBCXX_NODISCARD inline bool
     operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     { return !(__x == __y); }
 
   /// Based on operator<
   template<typename _Tp, typename _Alloc>
-    inline bool
+    _GLIBCXX_NODISCARD inline bool
     operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     { return __y < __x; }
 
   /// Based on operator<
   template<typename _Tp, typename _Alloc>
-    inline bool
+    _GLIBCXX_NODISCARD inline bool
     operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     { return !(__y < __x); }
 
   /// Based on operator<
   template<typename _Tp, typename _Alloc>
-    inline bool
+    _GLIBCXX_NODISCARD inline bool
     operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     { return !(__x < __y); }
 #endif // three-way comparison
index 5a0fc80865137e45cd54e6ccab75a7e6a0222a19..216822975a2f95abe5c6085a994ea331281c75af 100644 (file)
@@ -866,7 +866,7 @@ namespace __debug
     };
 
   template<typename _Tp, typename _Alloc>
-    _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<typename _Tp, typename _Alloc>
+    [[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<typename _Tp, typename _Alloc>
+    _GLIBCXX_NODISCARD
     inline bool
     operator!=(const vector<_Tp, _Alloc>& __lhs,
               const vector<_Tp, _Alloc>& __rhs)
     { return __lhs._M_base() != __rhs._M_base(); }
 
   template<typename _Tp, typename _Alloc>
+    _GLIBCXX_NODISCARD
     inline bool
     operator<(const vector<_Tp, _Alloc>& __lhs,
              const vector<_Tp, _Alloc>& __rhs)
     { return __lhs._M_base() < __rhs._M_base(); }
 
   template<typename _Tp, typename _Alloc>
+    _GLIBCXX_NODISCARD
     inline bool
     operator<=(const vector<_Tp, _Alloc>& __lhs,
               const vector<_Tp, _Alloc>& __rhs)
     { return __lhs._M_base() <= __rhs._M_base(); }
 
   template<typename _Tp, typename _Alloc>
+    _GLIBCXX_NODISCARD
     inline bool
     operator>=(const vector<_Tp, _Alloc>& __lhs,
               const vector<_Tp, _Alloc>& __rhs)
     { return __lhs._M_base() >= __rhs._M_base(); }
 
   template<typename _Tp, typename _Alloc>
+    _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 (file)
index 0000000..3b5480d
--- /dev/null
@@ -0,0 +1,153 @@
+// { dg-do compile { target c++17 } }
+
+#include <vector>
+
+void
+test_observers(std::vector<int> 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<float> 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<char> 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<S> 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<bool> 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<bool> 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<bool> 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<bool> 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" }
+}