Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH] gdbsupport: mark array_view::slice with [[nodiscard]]
Date: Thu,  2 Nov 2023 23:19:09 -0400	[thread overview]
Message-ID: <20231103031917.927026-1-simon.marchi@polymtl.ca> (raw)

From: Simon Marchi <simon.marchi@efficios.com>

I (almost) had a bug where I did:

    buffer.slice (...)

but I meant:

    buffer = buffer.slice (...)

The first one does nothing, it creates a new array_view but without
using it, it's useless.  Mark the slice methods with [[nodiscard]]
(which is standard C++17) so that error would generate a warning.

I guess that many functions could be marked as nodiscard, essentially
function that is pure (doesn't have side-effects).  But this one seems
particularly easy to mis-use.

Change-Id: Ib39a0a65a5728a3cfd68a02ae31635810baeaccb
---
 gdbsupport/array-view.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index ee3a3c58710c..4b519112e78f 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -185,6 +185,7 @@ class array_view
   /* Slice an array view.  */
 
   /* Return a new array view over SIZE elements starting at START.  */
+  [[nodiscard]]
   constexpr array_view<T> slice (size_type start, size_type size) const noexcept
   {
 #if defined(_GLIBCXX_DEBUG) && __cplusplus >= 201402L
@@ -195,6 +196,7 @@ class array_view
 
   /* Return a new array view over all the elements after START,
      inclusive.  */
+  [[nodiscard]]
   constexpr array_view<T> slice (size_type start) const noexcept
   {
 #if defined(_GLIBCXX_DEBUG) && __cplusplus >= 201402L

base-commit: 268109cad16c692e24a583c21ef5a8ac58cc51fe
-- 
2.42.0


             reply	other threads:[~2023-11-03  3:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-03  3:19 Simon Marchi [this message]
2023-11-03 18:12 ` Tom Tromey
2023-11-03 18:28   ` Simon Marchi
2023-11-03 18:33     ` Tom Tromey
2023-11-03 18:59       ` Simon Marchi

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=20231103031917.927026-1-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /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