From patchwork Tue Feb 27 11:42:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 207155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2640162dyb; Tue, 27 Feb 2024 03:48:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUO1llUgkALiZ7BjR/dglGIPhjs3ZuLkzXT9PNo1knyD82nfoizKQaRWqundFK077VPXW7J8xCkJxHmQvUEP2HRcrFm1w== X-Google-Smtp-Source: AGHT+IFAchaYmCVSdvpRxmiKoQN/e0Ocfn4dqT/CvTjEuYvj3BpSd4ON16220FKV/q5oPJf8Evhk X-Received: by 2002:a0c:e303:0:b0:68f:2e24:6156 with SMTP id s3-20020a0ce303000000b0068f2e246156mr1605449qvl.16.1709034499524; Tue, 27 Feb 2024 03:48:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709034499; cv=pass; d=google.com; s=arc-20160816; b=YbNBX4uMcnpywgHpE/mwm7pKsj1V5YHUR5btv90odX5uAVYPG34hLaD7aTzfrjROif COvLCWhBkLHkiAFNXP2RZQpphkanKppfYq9JO6x+bryWEnTLi15EovdQZesiFzMfm/Wg NA/hy5WaGMtXZWgoEt+xWwhJC6BJacKLTgJGrKTaKa2FUvkibDEVPfIx0c2bqVYjG7gw klRn9PGc9hYqQz2TGGvV66RIiidEGtDPAb+s0xXVwnQ4lGr8harXo2YCQi7yr38giitN c6XYwZkiL9PwMiCIEsK9UxpRsaIBNemqZoaPd91uY0WVsiDWYXzIqBMB6nDmdmwfBMxd T9Wg== 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=4XOU0x8sB993T8dA4cmkWaoY/C/Qb4Zkj/yDaNQhNe8=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=hxO3/UAjSDcy5lFUAjAD0HAayTl5UDlmxIcnAu3P2xjyg/4Ik9ri6oBONon+qxOWGL W5dYlnkN9BR5x/mk3LFc6Ak9mgKzLgttTGJr1JWr2hhsStcLB/7dtQeLhXluLxsj0N9P t50cpjw1HnOP01LEEmxAWf837fINM7lRFQSXD+FniEgWPvHJ18WwtynQe/LoLdcD9wLX FoL9chviTuZoo7yI0A4WYCWOTR3n5gN3mPov+srP6l8wsLCjh4gI/98+mwJuW2+AVM7k 0U/icIrx9acXet5MkqZKAF5SWCJPueUaGGaFNnYZ0CIJ5JKbw8AZz9JvoxaQ8WS7Aez/ V2xg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DGtGjmJV; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id jp9-20020ad45f89000000b0068f555f32b8si7398820qvb.491.2024.02.27.03.48.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 03:48:19 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DGtGjmJV; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 3A07538582B9 for ; Tue, 27 Feb 2024 11:48:19 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id B69F9385841D for ; Tue, 27 Feb 2024 11:45:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B69F9385841D 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 B69F9385841D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709034339; cv=none; b=jtKNM+4SaX1kfKrtIGwx8xF3mGRSGxSwGkwXBiei0WFcoSzZ6fsXUIgQ/vTGtPn26dcxDnUfGP02ZsLYmh3bzocEOMmXZw5v4O3ho/gCHIhNgfpDY49yMnlQvz8Jj1liR3nRm0cUMaKVLGxqWnMZh2Pqhy3JK+LZ8sG1ewETiwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709034339; c=relaxed/simple; bh=C5vJlnmpGUAJnC7Uc1t8GdX0L54eib08CQ4Tj1w7cpU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uerszd3b5BSJjxDRNwBp5LYAgMnJQaMx5tdIhjph4+6ArbTZ0z3S6hWz/HBn7CHAXVLWRfZnFwCsWEiH95lDa3DUA9x7wr/y5XH8rzQorgItnBJs64/+itBvGocARgm54lfcGhjd44zlqbW9itXvCsNV1XNHd65Q7Y88Un2bMDI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709034335; 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=4XOU0x8sB993T8dA4cmkWaoY/C/Qb4Zkj/yDaNQhNe8=; b=DGtGjmJVlaNw17HvFFdfrHFhTaXJ3jHFRwIixC47u7OLQ624fAy757x1J9MvVyP+IcDEBr prCtY7HsdPQC+Bbv6s5zuGuqIpl+g4a149MQegDyxegoU0cwWE0UBPVNi8mJiGQnoNk4Ly wkVMU0SsW4ZjG2GA9IKXsx7/Ew4kr14= 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-657-_WJiuBcNMI6OWvb35-9dBg-1; Tue, 27 Feb 2024 06:45:34 -0500 X-MC-Unique: _WJiuBcNMI6OWvb35-9dBg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 C690588B766; Tue, 27 Feb 2024 11:45:33 +0000 (UTC) Received: from localhost (unknown [10.42.28.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id 922AEC185C0; Tue, 27 Feb 2024 11:45:33 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 6/8] libstdc++: Add more nodiscard uses in Date: Tue, 27 Feb 2024 11:42:03 +0000 Message-ID: <20240227114528.1350601-6-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 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_H2, 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: 1792052559082127895 X-GMAIL-MSGID: 1792052559082127895 Tested x86_64-linux. Reviews invited. -- >8 -- 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>=): 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 - _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/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" } +}