public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [PATCH 6/8] libstdc++: Add more nodiscard uses in <vector>
Date: Tue, 27 Feb 2024 11:42:03 +0000	[thread overview]
Message-ID: <20240227114528.1350601-6-jwakely@redhat.com> (raw)

Tested x86_64-linux. Reviews invited.

-- >8 --

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>=): Add nodiscard.
	* testsuite/23_containers/vector/nodiscard.cc: New test.
---
 libstdc++-v3/include/bits/stl_bvector.h       |   4 +-
 libstdc++-v3/include/bits/stl_vector.h        |  18 +--
 .../23_containers/vector/nodiscard.cc         | 153 ++++++++++++++++++
 3 files changed, 164 insertions(+), 11 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/23_containers/vector/nodiscard.cc

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<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
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 <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" }
+}
-- 
2.43.0


                 reply	other threads:[~2024-02-27 11:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240227114528.1350601-6-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).