Hi! As found by Jonathan, we shouldn't be trying to emit return *this; fixit hint in non-methods where this is not available (and ICEing because current_class_ref is NULL there). I've just added a testcase for Jon's patch from the PR and merged two nested ifs into one.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2019-01-04 Jonathan Wakely <jwak...@redhat.com> Jakub Jelinek <ja...@redhat.com> PR c++/88554 * decl.c (finish_function): For -Wreturn-type don't add a return *this; fixit hint if current_class_ref is NULL. Use a single if instead of two nested ones. * g++.dg/warn/Wreturn-type-11.C: New test. --- gcc/cp/decl.c.jj 2019-01-03 11:11:11.568612960 +0100 +++ gcc/cp/decl.c 2019-01-04 20:33:39.258819779 +0100 @@ -16094,11 +16094,12 @@ finish_function (bool inline_p) { tree valtype = TREE_TYPE (DECL_RESULT (fndecl)); if (TREE_CODE (valtype) == REFERENCE_TYPE + && current_class_ref && same_type_ignoring_top_level_qualifiers_p - (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) - if (global_dc->option_enabled (OPT_Wreturn_type, - global_dc->option_state)) - add_return_star_this_fixit (&richloc, fndecl); + (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)) + && global_dc->option_enabled (OPT_Wreturn_type, + global_dc->option_state)) + add_return_star_this_fixit (&richloc, fndecl); } warning_at (&richloc, OPT_Wreturn_type, "no return statement in function returning non-void"); --- gcc/testsuite/g++.dg/warn/Wreturn-type-11.C.jj 2019-01-04 20:34:18.062185207 +0100 +++ gcc/testsuite/g++.dg/warn/Wreturn-type-11.C 2019-01-04 20:35:36.048909843 +0100 @@ -0,0 +1,11 @@ +// PR c++/88554 +// { dg-do compile } +// { dg-options "-Wreturn-type" } + +struct X { + friend X & operator+= (X &, int) { } // { dg-warning "no return statement in function returning non-void" } + // { dg-bogus "return \\*this;" "" { target *-*-* } .-1 } +}; +struct Y {}; +Y & operator += (Y &, Y &) { } // { dg-warning "no return statement in function returning non-void" } + // { dg-bogus "return \\*this;" "" { target *-*-* } .-1 } Jakub