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

--- Comment #5 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #3)
> If we add [[nodiscard]] to the constructors of the standard exception types
> then we get a warning with no compiler changes needed:

Is it possible to do this to the standard library?  Even if it is, maybe a
warning for discarded temporary of a type that inherits from std::exception is
still useful.

A possible wording:

  warning: discarding temporary exception object; did you mean to `throw' it?
[-Wmissing-throw]
      std::invalid_argument ("division by zero");
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      <fix-it hint suggesting prepending "throw ">

  note: `std::invalid_argument' is derived from `std::exception'
      <show the decl of std::invalid_argument>

Probably shouldn't issue this warning for e.g.: named local that you forgot to
throw:

#include <stdexcept>

int divide(int num, int denom) {
    std::invalid_argument exc ("division by zero");
    return num / denom;
}

since the unused-variable warning should detect it.

Reply via email to