[Bug c++/85736] New: Support warn_unused or warn_unused_result on specific constructors

redbeard0531 at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu May 10 16:29:00 GMT 2018


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

            Bug ID: 85736
           Summary: Support warn_unused or warn_unused_result on specific
                    constructors
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

It would be nice to get the benefits of those attributes on a per-constructor
basis, rather than requiring them on the whole type. The particular use case I
have in mind is for unique_lock's default constructor (or at least on our
wrapper around it). I recently did a code review where someone typed:

std::unique_lock<std::mutex> myMutex;

where they meant to use:

std::unique_lock<std::mutex> lk(myMutex);

There is currently no warning for this at -Wall -Wextra, although thankfully it
is at least caught when myMutex has parentheses around it, which is the more
common mistake. Clearly, it wouldn't make sense to put warn_unused on the whole
unique_lock since the second line is fine.

It would probably make sense on almost all default constructors actually, since
with the exception of a few specific types that alter global or thread-local
state, why are you declaring a default constructed variable then not using it
at all? But on a few types like unique_lock it seems actively dangerous rather
than just simply wasteful.


More information about the Gcc-bugs mailing list