This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/85973] New: [[nodiscard]] shall emit a warning for unused anonymous variable


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85973

            Bug ID: 85973
           Summary: [[nodiscard]] shall emit a warning for unused
                    anonymous variable
           Product: gcc
           Version: 8.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: romain.geissler at amadeus dot com
  Target Milestone: ---

Hi,

I would to know if the following (currently not implemented) behaviour of
[[nodiscard]] would be useful or not. I am raising this since we have just hit
this case in a real codebase.

Currently a class can be annotated [[nodiscard]] in which case you can't ignore
returned values of this class. However no warning is emitted when an anonymous
and apparently unused instance of this class is created.

It will be clearer with the following snippet:

class [[nodiscard]] A
{
    public:
        A(void* someArgument) { /* Get some RAII resources */}

        ~A() { /* Release some RAII resources */}

        A(A&&) = default;
        A& operator=(A&&) = default;

    private:
        A(const A&) = delete;
        A& operator=(const A&) = delete;
};

A f();

void someStatement();

void g()
{
    f(); // Here ignored A result is correctly warned

    {
        A(nullptr); // Here the anonymous A created does not yield any warning
        // while one might expect to (ie we should write: A a(nullptr); using a
        // non anonymous variable, which changes the semantic of the program.)

        someStatement(); // statement using the fact that we use RAII resource
locking.
    }
}

Typically A could be std::lock_guard.

As you can see in godbolt https://godbolt.org/g/AdxJMY only the call to f()
raises the warning, but not the anonymous A(nullptr). One (actually I) would
expect also a warning on the line A(nullptr). Is this improvement welcome or
not ?

Cheers,
Romain

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]