From patchwork Sat Nov 11 00:44:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 164048 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp1500373vqs; Fri, 10 Nov 2023 18:01:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeroK8+zClQm5SPnk1wFaikFdmTv2J/iJJ8v7uoXkV2e1XOkxhC4qFO0/USP/jLwlX9ktM X-Received: by 2002:a05:620a:25ca:b0:775:806d:5389 with SMTP id y10-20020a05620a25ca00b00775806d5389mr1043457qko.70.1699668092366; Fri, 10 Nov 2023 18:01:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699668092; cv=pass; d=google.com; s=arc-20160816; b=gFHjw+AQwFIrb8tvGL7PArEpG7TCTwwdU2fxPg/1QGEJsQG2ZF/GRRNbaRSB7v/YjU bo8ERYYMFORSpZlWQGc+bAQQpLAUaa/k9ux8uQe8mIvollxYmyBLRcuUtve/BivU9e7V Pvi6gydrbc82z2JRWFq5DgqzT6IIGprHb1Wt1VVP9oTGDsbRLJ1/A2AJzFN1OupseQdZ VMM+Q9VNtqz/RC0nFgFZUMPp8BvSlJigAZ8+2OLgnaj/GNYrRHD/uZoYnyV0PmqT0g4s XueGaxbooiBTO/UWN2pXfQTXHA908RYmg6N/ivQqr4mLpH80Js0BRNbx7w5sH/n5MrKq uLhA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=PS/Kj9Vy5Mi6G4FLgOhOELS+OckDP9YO17HA7f3gbtQ=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=teHVXWUIHaAb6hOUZ29BzkLv+0cpb1xx6/77LwKfPN3GGw85/2mCBBTxvJ8HreRbDJ SW5KC4o1xLljiiXQXHrYyiHkaxVjCIEeo3rHxKv66WEOugT6lowkdIjY/HpwjAlTynmF erjqguXM7SNVVPo7ZjKJbvSanNnZucRICIFQ6Pybtvr3JTBCmpccErO6mXMJe6meDcWf A5DnCEGoxZGaa+W6jjTs4lcrIDGfHAk6VDaoHUOYh8wvuk0/vecl06Avq/2sikzo3FUU /4Ua7V3AE9B+QqSxVeoyh8zsByos5Xx3o1G7pqF25FIEZkYDy/0Zcs7rLkzOt1WYLOKy Pv0A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fnvMhvC+; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m16-20020a05620a291000b00775902632ffsi669371qkp.279.2023.11.10.18.01.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 18:01:32 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fnvMhvC+; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 42C703944401 for ; Sat, 11 Nov 2023 00:45:42 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 377DF382A2F5 for ; Sat, 11 Nov 2023 00:44:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 377DF382A2F5 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 377DF382A2F5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699663488; cv=none; b=pMRNxkmziIwfBhR/Pn6LaETQyLdfvmOhbt7IXwqJBWQRq4yDqTiTaz39uUuyqrxHV+ziIXnGw4vY1//dKlkJRcoGHRq3tivxHk0itOZre2Bmquv3nl06OZJPHrFe/UghcoyyoxjWDQX59eYpT/yMt1IzF1yXpKdm3PzRE6Jp5ic= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699663488; c=relaxed/simple; bh=x+YMlC1LHLpWSlb8EcLnWs71gFxdx4et/f8Si+Ox/B0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XJMac1OoDE8b15jiAyYjdb76fsLmvw19seHG8BAf1UVRo4JFDXGB3QyIy85DEG9Vl4+cIwmraqkGLeyLChMeKl+14kpAfpqeaF5gGcBNSI1f8NVjLof10othxlE02M1LO738bZ9G88GhKO6xmbdz+xDRflVeWUbCDbVZaKe9JYg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699663482; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PS/Kj9Vy5Mi6G4FLgOhOELS+OckDP9YO17HA7f3gbtQ=; b=fnvMhvC+5ab3l7x/4avVgJ1LvkNT9dhA6Qb5DnGO6tMK1WcBNV0HTX8ynWbdcEGaHWksaq Lrh/bK7tKRwbeiUHIo/uzFaIxyyzevJwd8+E6q/TCmvLJaxgrVUSom4fvCQYiFUCPygA90 SdSFl2t5D2XD5OAm+0jPRQPeIQLrezU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-412-awkwGn7oM3q7YHXkGCfqVg-1; Fri, 10 Nov 2023 19:44:41 -0500 X-MC-Unique: awkwGn7oM3q7YHXkGCfqVg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1B929185A780; Sat, 11 Nov 2023 00:44:41 +0000 (UTC) Received: from localhost (unknown [10.42.28.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1780502A; Sat, 11 Nov 2023 00:44:40 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add [[nodiscard]] to std::span members Date: Sat, 11 Nov 2023 00:44:35 +0000 Message-ID: <20231111004440.77760-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782231169368419521 X-GMAIL-MSGID: 1782231169368419521 Tested x86_64-linux. Pushed to trunk. -- >8 -- All std::span member functions are pure functions that have no side effects. They are only useful for their return value, so they should all warn if that value is not used. libstdc++-v3/ChangeLog: * include/std/span (span, as_bytes, as_writable_bytes): Add [[nodiscard]] attribute on all non-void functions. * testsuite/23_containers/span/back_assert_neg.cc: Suppress nodiscard warning. * testsuite/23_containers/span/back_neg.cc: Likewise. * testsuite/23_containers/span/first_2_assert_neg.cc: Likewise. * testsuite/23_containers/span/first_assert_neg.cc: Likewise. * testsuite/23_containers/span/first_neg.cc: Likewise. * testsuite/23_containers/span/front_assert_neg.cc: Likewise. * testsuite/23_containers/span/front_neg.cc: Likewise. * testsuite/23_containers/span/index_op_assert_neg.cc: Likewise. * testsuite/23_containers/span/index_op_neg.cc: Likewise. * testsuite/23_containers/span/last_2_assert_neg.cc: Likewise. * testsuite/23_containers/span/last_assert_neg.cc: Likewise. * testsuite/23_containers/span/last_neg.cc: Likewise. * testsuite/23_containers/span/subspan_2_assert_neg.cc: Likewise. * testsuite/23_containers/span/subspan_3_assert_neg.cc: Likewise. * testsuite/23_containers/span/subspan_4_assert_neg.cc: Likewise. * testsuite/23_containers/span/subspan_5_assert_neg.cc: Likewise. * testsuite/23_containers/span/subspan_6_assert_neg.cc: Likewise. * testsuite/23_containers/span/subspan_assert_neg.cc: Likewise. * testsuite/23_containers/span/subspan_neg.cc: Likewise. * testsuite/23_containers/span/nodiscard.cc: New test. --- libstdc++-v3/include/std/span | 26 ++++++++- .../23_containers/span/back_assert_neg.cc | 2 +- .../testsuite/23_containers/span/back_neg.cc | 2 +- .../23_containers/span/first_2_assert_neg.cc | 2 +- .../23_containers/span/first_assert_neg.cc | 2 +- .../testsuite/23_containers/span/first_neg.cc | 2 +- .../23_containers/span/front_assert_neg.cc | 2 +- .../testsuite/23_containers/span/front_neg.cc | 2 +- .../23_containers/span/index_op_assert_neg.cc | 2 +- .../23_containers/span/index_op_neg.cc | 2 +- .../23_containers/span/last_2_assert_neg.cc | 2 +- .../23_containers/span/last_assert_neg.cc | 2 +- .../testsuite/23_containers/span/last_neg.cc | 2 +- .../testsuite/23_containers/span/nodiscard.cc | 58 +++++++++++++++++++ .../span/subspan_2_assert_neg.cc | 2 +- .../span/subspan_3_assert_neg.cc | 2 +- .../span/subspan_4_assert_neg.cc | 2 +- .../span/subspan_5_assert_neg.cc | 2 +- .../span/subspan_6_assert_neg.cc | 2 +- .../23_containers/span/subspan_assert_neg.cc | 2 +- .../23_containers/span/subspan_neg.cc | 6 +- 21 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/span/nodiscard.cc diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span index d5644a196a2..90d08f18d2c 100644 --- a/libstdc++-v3/include/std/span +++ b/libstdc++-v3/include/std/span @@ -246,20 +246,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // observers + [[nodiscard]] constexpr size_type size() const noexcept { return this->_M_extent._M_extent(); } + [[nodiscard]] constexpr size_type size_bytes() const noexcept { return this->_M_extent._M_extent() * sizeof(element_type); } - [[nodiscard]] constexpr bool + [[nodiscard]] + constexpr bool empty() const noexcept { return size() == 0; } // element access + [[nodiscard]] constexpr reference front() const noexcept { @@ -267,6 +271,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this->_M_ptr; } + [[nodiscard]] constexpr reference back() const noexcept { @@ -274,6 +279,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *(this->_M_ptr + (size() - 1)); } + [[nodiscard]] constexpr reference operator[](size_type __idx) const noexcept { @@ -281,41 +287,50 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *(this->_M_ptr + __idx); } + [[nodiscard]] constexpr pointer data() const noexcept { return this->_M_ptr; } // iterator support + [[nodiscard]] constexpr iterator begin() const noexcept { return iterator(this->_M_ptr); } + [[nodiscard]] constexpr iterator end() const noexcept { return iterator(this->_M_ptr + this->size()); } + [[nodiscard]] constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(this->end()); } + [[nodiscard]] constexpr reverse_iterator rend() const noexcept { return reverse_iterator(this->begin()); } #if __cplusplus > 202002L + [[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); } + [[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); } + [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); } + [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept { return rend(); } @@ -324,6 +339,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // subviews template + [[nodiscard]] constexpr span first() const noexcept { @@ -335,6 +351,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return _Sp{ this->data(), _Count }; } + [[nodiscard]] constexpr span first(size_type __count) const noexcept { @@ -343,6 +360,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template + [[nodiscard]] constexpr span last() const noexcept { @@ -354,6 +372,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return _Sp{ this->data() + (this->size() - _Count), _Count }; } + [[nodiscard]] constexpr span last(size_type __count) const noexcept { @@ -362,6 +381,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template + [[nodiscard]] constexpr auto subspan() const noexcept -> span()> @@ -393,6 +413,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } + [[nodiscard]] constexpr span subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept @@ -434,6 +455,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION -> span>>; template + [[nodiscard]] inline span @@ -451,7 +473,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline span - as_writable_bytes(span<_Type, _Extent> __sp) noexcept + as_writable_bytes [[nodiscard]] (span<_Type, _Extent> __sp) noexcept { auto data = reinterpret_cast(__sp.data()); auto size = __sp.size_bytes(); diff --git a/libstdc++-v3/testsuite/23_containers/span/back_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/back_assert_neg.cc index b42aea3b4b4..71480246fed 100644 --- a/libstdc++-v3/testsuite/23_containers/span/back_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/back_assert_neg.cc @@ -25,5 +25,5 @@ int main() { std::span s; - s.back(); + (void) s.back(); } diff --git a/libstdc++-v3/testsuite/23_containers/span/back_neg.cc b/libstdc++-v3/testsuite/23_containers/span/back_neg.cc index d3ea4f405f4..dce512aa1c8 100644 --- a/libstdc++-v3/testsuite/23_containers/span/back_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/back_neg.cc @@ -24,7 +24,7 @@ test01(bool b) { std::span s; if (b || !s.empty()) - s.back(); + (void) s.back(); return true; } diff --git a/libstdc++-v3/testsuite/23_containers/span/first_2_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/first_2_assert_neg.cc index 876a6b773d6..d885268c96c 100644 --- a/libstdc++-v3/testsuite/23_containers/span/first_2_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/first_2_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.first(5); + (void) s.first(5); } diff --git a/libstdc++-v3/testsuite/23_containers/span/first_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/first_assert_neg.cc index f1d8cdccb9b..829818c65c2 100644 --- a/libstdc++-v3/testsuite/23_containers/span/first_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/first_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.first<5>(); + (void) s.first<5>(); } diff --git a/libstdc++-v3/testsuite/23_containers/span/first_neg.cc b/libstdc++-v3/testsuite/23_containers/span/first_neg.cc index 4a8cd184172..c47b8db7281 100644 --- a/libstdc++-v3/testsuite/23_containers/span/first_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/first_neg.cc @@ -24,6 +24,6 @@ test01() { int a[4]; std::span s(a); - s.first<5>(); // { dg-error "here" } + (void) s.first<5>(); // { dg-error "here" } } // { dg-error "static assertion failed" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/23_containers/span/front_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/front_assert_neg.cc index 12cbd4a80ae..5dd9c963677 100644 --- a/libstdc++-v3/testsuite/23_containers/span/front_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/front_assert_neg.cc @@ -25,5 +25,5 @@ int main() { std::span s; - s.front(); + (void) s.front(); } diff --git a/libstdc++-v3/testsuite/23_containers/span/front_neg.cc b/libstdc++-v3/testsuite/23_containers/span/front_neg.cc index b5525db8f1d..b2c5e9cae32 100644 --- a/libstdc++-v3/testsuite/23_containers/span/front_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/front_neg.cc @@ -24,7 +24,7 @@ test01(bool b) { std::span s; if (b || !s.empty()) - s.front(); + (void) s.front(); return true; } diff --git a/libstdc++-v3/testsuite/23_containers/span/index_op_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/index_op_assert_neg.cc index c75924e5f38..9a62bdd3233 100644 --- a/libstdc++-v3/testsuite/23_containers/span/index_op_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/index_op_assert_neg.cc @@ -25,5 +25,5 @@ int main() { std::span s; - s[99]; + (void) s[99]; } diff --git a/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc b/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc index 6e8cd4903b3..a2cdb8a44c7 100644 --- a/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc @@ -24,7 +24,7 @@ test01(bool b) { std::span s; if (b || !s.empty()) - s[99]; + (void) s[99]; return true; } diff --git a/libstdc++-v3/testsuite/23_containers/span/last_2_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/last_2_assert_neg.cc index a464fa516b0..86439392e2c 100644 --- a/libstdc++-v3/testsuite/23_containers/span/last_2_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/last_2_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.last(5); + (void) s.last(5); } diff --git a/libstdc++-v3/testsuite/23_containers/span/last_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/last_assert_neg.cc index f36db455519..a32246ada47 100644 --- a/libstdc++-v3/testsuite/23_containers/span/last_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/last_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.last<5>(); + (void) s.last<5>(); } diff --git a/libstdc++-v3/testsuite/23_containers/span/last_neg.cc b/libstdc++-v3/testsuite/23_containers/span/last_neg.cc index 2e86233f19a..c85668ab128 100644 --- a/libstdc++-v3/testsuite/23_containers/span/last_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/last_neg.cc @@ -24,6 +24,6 @@ test01() { int a[2]; std::span s(a); - s.last<3>(); // { dg-error "here" } + (void) s.last<3>(); // { dg-error "here" } } // { dg-error "static assertion failed" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/23_containers/span/nodiscard.cc b/libstdc++-v3/testsuite/23_containers/span/nodiscard.cc new file mode 100644 index 00000000000..3bccd1545c3 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/span/nodiscard.cc @@ -0,0 +1,58 @@ +// { dg-do compile { target c++20 } } + +#include + +void +test_observers(std::span s) +{ + s.size(); // { dg-warning "ignoring return value" } + s.size_bytes(); // { dg-warning "ignoring return value" } + s.empty(); // { dg-warning "ignoring return value" } +} + +void +test_element_access(std::span s) +{ + s.front(); // { dg-warning "ignoring return value" } + s.back(); // { dg-warning "ignoring return value" } + s[1]; // { dg-warning "ignoring return value" } + s.data(); // { dg-warning "ignoring return value" } +} + +struct S { }; + +void +test_iterators(std::span s) +{ + s.begin(); // { dg-warning "ignoring return value" } + s.end(); // { dg-warning "ignoring return value" } + s.rbegin(); // { dg-warning "ignoring return value" } + s.rend(); // { dg-warning "ignoring return value" } + +#if __cplusplus > 202002L + s.cbegin(); // { dg-warning "ignoring return value" "" { target c++23 } } + s.cend(); // { dg-warning "ignoring return value" "" { target c++23 } } + s.crbegin(); // { dg-warning "ignoring return value" "" { target c++23 } } + s.crend(); // { dg-warning "ignoring return value" "" { target c++23 } } +#endif +} + +void +test_subviews(std::span s) +{ + s.first<5>(); // { dg-warning "ignoring return value" } + s.first(6); // { dg-warning "ignoring return value" } + s.last<7>(); // { dg-warning "ignoring return value" } + s.last(8); // { dg-warning "ignoring return value" } + s.subspan<1>(); // { dg-warning "ignoring return value" } + s.subspan<2, 3>(); // { dg-warning "ignoring return value" } + s.subspan(4); // { dg-warning "ignoring return value" } + s.subspan(5, 6); // { dg-warning "ignoring return value" } +} + +void +test_non_members(std::span s) +{ + std::as_bytes(s); // { dg-warning "ignoring return value" } + std::as_writable_bytes(s); // { dg-warning "ignoring return value" } +} diff --git a/libstdc++-v3/testsuite/23_containers/span/subspan_2_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/subspan_2_assert_neg.cc index 12b79fffc5d..3151f544a24 100644 --- a/libstdc++-v3/testsuite/23_containers/span/subspan_2_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/subspan_2_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.subspan<2, 5>(); + (void) s.subspan<2, 5>(); } diff --git a/libstdc++-v3/testsuite/23_containers/span/subspan_3_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/subspan_3_assert_neg.cc index 3130a503534..4bb29fe99d6 100644 --- a/libstdc++-v3/testsuite/23_containers/span/subspan_3_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/subspan_3_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.subspan<2, 3>(); + (void) s.subspan<2, 3>(); } diff --git a/libstdc++-v3/testsuite/23_containers/span/subspan_4_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/subspan_4_assert_neg.cc index 2a57ddf48b8..eee4d558feb 100644 --- a/libstdc++-v3/testsuite/23_containers/span/subspan_4_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/subspan_4_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.subspan(5, 0); + (void) s.subspan(5, 0); } diff --git a/libstdc++-v3/testsuite/23_containers/span/subspan_5_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/subspan_5_assert_neg.cc index 48aaeaffbbf..705359d504e 100644 --- a/libstdc++-v3/testsuite/23_containers/span/subspan_5_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/subspan_5_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.subspan(2, 5); + (void) s.subspan(2, 5); } diff --git a/libstdc++-v3/testsuite/23_containers/span/subspan_6_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/subspan_6_assert_neg.cc index 2c90c5e67f8..21715a1a343 100644 --- a/libstdc++-v3/testsuite/23_containers/span/subspan_6_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/subspan_6_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.subspan(2, 3); + (void) s.subspan(2, 3); } diff --git a/libstdc++-v3/testsuite/23_containers/span/subspan_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/subspan_assert_neg.cc index 4873c757996..ee35e874b6d 100644 --- a/libstdc++-v3/testsuite/23_containers/span/subspan_assert_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/subspan_assert_neg.cc @@ -26,5 +26,5 @@ int main() { int a[4]; std::span s(a); - s.subspan<5, 0>(); + (void) s.subspan<5, 0>(); } diff --git a/libstdc++-v3/testsuite/23_containers/span/subspan_neg.cc b/libstdc++-v3/testsuite/23_containers/span/subspan_neg.cc index 3c69f236755..8c1d2bbfeef 100644 --- a/libstdc++-v3/testsuite/23_containers/span/subspan_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/subspan_neg.cc @@ -24,7 +24,7 @@ test01() { int a[4]; std::span s(a); - s.subspan<5, 0>(); // { dg-error "here" } + (void) s.subspan<5, 0>(); // { dg-error "here" } } void @@ -32,7 +32,7 @@ test02() { int a[4]; std::span s(a); - s.subspan<3, 5>(); // { dg-error "here" } + (void) s.subspan<3, 5>(); // { dg-error "here" } } void @@ -40,7 +40,7 @@ test03() { int a[4]; std::span s(a); - s.subspan<3, 2>(); // { dg-error "here" } + (void) s.subspan<3, 2>(); // { dg-error "here" } } // { dg-error "static assertion failed" "" { target *-*-* } 0 }