On 25/04/18 12:22 -0400, David Malcolm wrote:
On Wed, 2018-04-25 at 16:54 +0100, Jonathan Wakely wrote:
On 25/04/18 16:30 +0100, Andrew Haley wrote:
> On 04/25/2018 03:04 PM, Jonathan Wakely wrote:
> > On 25/04/18 14:59 +0100, Andrew Haley wrote:
> > > On 04/25/2018 02:56 PM, Jason Merrill wrote:
> > > > The warning by default seems sufficient to me.
> > >
> > > Yes.  We've been bitten by this a few times, with mysterious
> > > crashes.
> > > I'm not sure it even makes sense only to be a warning, but I
> > > guess
> > > that's up to the C++ TC.
> >
> > It's not always possible for the compiler to prove that flowing
> > off
> > the end never happens, even if the program state ensures that it
> > can't
> > (e.g. by all callers enforcing the function's preconditions
> > correctly). So making it ill-formed is deemed too draconian
> > whenever
> > this gets discussed.
>
> Sure.  Having said that, the cases that bit me were those where
> control
> always flowed off the end, i.e. the function contained no return
> statement.

I forget the "return *this;" in assignment operators embarrassingly
often. So often I've even contemplated a proposal to define flowing
off the end of an assignment operator equivalent to "return *this;"

Could/should we offer a fix-it hint for such cases?

Yes. For the general "missing return" that -Wreturn-type warns about
we can't know what a sensible return value would be. For assignment
operators 99.999% of them return *this and so a fix-it suggestion to
add exactly that would be good. This works:

@@ -15869,6 +15851,12 @@ finish_function (bool inline_p)
    {
      warning (OPT_Wreturn_type,
              "no return statement in function returning non-void");
+      if (DECL_NAME (fndecl) == assign_op_identifier)
+       {
+         rich_location richloc (line_table, input_location);
+         richloc.add_fixit_insert_before (input_location, "return *this;");
+         inform (&richloc, "Oi, Wakely, you forgot to return something");
+       }
      TREE_NO_WARNING (fndecl) = 1;
    }


But it should really check if *this is convertible to the return type,
just in case we have something unusual like:

struct X { int operator=(int) { } };

Reply via email to