[committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers

Jonathan Wakely jwakely@redhat.com
Thu Aug 5 14:21:19 GMT 2021


On Thu, 5 Aug 2021 at 13:14, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
>
> On Thu, 5 Aug 2021 at 15:11, Christophe Lyon via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > Hi Jonathan,
> >
> > On Wed, Aug 4, 2021 at 2:04 PM Jonathan Wakely via Gcc-patches <
> > gcc-patches@gcc.gnu.org> wrote:
> >
> > > On 04/08/21 12:56 +0100, Jonathan Wakely wrote:
> > > >... and container adaptors.
> > > >
> > > >This adds the [[nodiscard]] attribute to functions with no side-effects
> > > >for the sequence containers and their iterators, and the debug versions
> > > >of those containers, and the container adaptors,
> > >
> > > I don't plan to add any more [[nodiscard]] attributes for now, but
> > > these two commits should demonstrate how to do it for anybody who
> > > wants to contribute similar patches.
> > >
> > > I didn't add tests that verify we do actually warn on each of those
> > > functions, because there are hundreds of them, and I know they're
> > > working because I had to alter existing tests to not warn.
> > >
> > >
> > I've noticed a regression on aarch64/arm:
> > FAIL: g++.old-deja/g++.other/inline7.C  -std=gnu++17 (test for excess
> > errors)
> > Excess errors:
> > /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring
> > return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type
> > std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc =
> > std::allocator<int*>; std::__cxx11::list<_Tp, _Alloc>::size_type = long
> > unsigned int]', declared with attribute 'nodiscard' [-Wunused-result]
> >
> > FAIL: g++.old-deja/g++.other/inline7.C  -std=gnu++2a (test for excess
> > errors)
> > Excess errors:
> > /gcc/testsuite/g++.old-deja/g++.other/inline7.C:11:11: warning: ignoring
> > return value of 'std::__cxx11::list<_Tp, _Alloc>::size_type
> > std::__cxx11::list<_Tp, _Alloc>::size() const [with _Tp = int*; _Alloc =
> > std::allocator<int*>; std::__cxx11::list<_Tp, _Alloc>::size_type = long
> > unsigned int]', declared with attribute 'nodiscard' [-Wunused-result]
> >
> > Not sure why you didn't see it?
>
> That can easily happen when running just the library tests, rather
> than all of them. :P

Right, I didn't run all the compiler tests.

Fixed with this patch, tested x86_64-linux, pushed to trunk.
-------------- next part --------------
commit 03d47da7e1e91adddbde261ffefd2760df59a564
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Aug 5 14:00:35 2021

    testsuite: Fix warning introduced by nodiscard in libstdc++
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    gcc/testsuite/ChangeLog:
    
            * g++.old-deja/g++.other/inline7.C: Cast nodiscard call to void.

diff --git a/gcc/testsuite/g++.old-deja/g++.other/inline7.C b/gcc/testsuite/g++.old-deja/g++.other/inline7.C
index a3723cfba1e..626000039c5 100644
--- a/gcc/testsuite/g++.old-deja/g++.other/inline7.C
+++ b/gcc/testsuite/g++.old-deja/g++.other/inline7.C
@@ -8,7 +8,7 @@ std::list<int*> li;
 
 void f ()
 {
-  li.size ();
+  (void) li.size ();
 }
 
 int main ()


More information about the Libstdc++ mailing list