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: 1905022 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DGtGjmJV; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TkbMV423Dz23qP for ; Tue, 27 Feb 2024 22:47:50 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 55A0B385800D for ; Tue, 27 Feb 2024 11:47:48 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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" } +}