[Bug c++/85973] New: [[nodiscard]] shall emit a warning for unused anonymous variable
romain.geissler at amadeus dot com
gcc-bugzilla@gcc.gnu.org
Tue May 29 13:11:00 GMT 2018
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
More information about the Gcc-bugs
mailing list