As discussed at https://gcc.gnu.org/ml/gcc/2018-04/msg00170.html we could offer fix-it hints for cases like: struct X { X& operator=(const X&) { } // show fix-it }; struct Y { void operator=(const Y&) { } int operator=(int) { } // no fix-it here, just a -Wreturn-type warning }; struct Z { Z& operator=(const Z& z) { i = z.i; } // show fix-it int i; };
Created attachment 44017 [details] Proof-of-concept patch to add fix-it
With that patch -fdiagnostics-generate-patch produces: ret.cc: In member function 'X& X::operator=(const X&)': ret.cc:2:28: warning: no return statement in function returning non-void [-Wreturn-type] X& operator=(const X&) { } // show fix-it ^ ret.cc:2:28: note: assignment operators usually return *this X& operator=(const X&) { } // show fix-it ^ return *this; ret.cc: In member function 'int Y::operator=(int)': ret.cc:7:24: warning: no return statement in function returning non-void [-Wreturn-type] int operator=(int) { } // no fix-it here, just a -Wreturn-type warning ^ ret.cc: In member function 'Z& Z::operator=(const Z&)': ret.cc:13:3: warning: no return statement in function returning non-void [-Wreturn-type] } // show fix-it ^ ret.cc:13:3: note: assignment operators usually return *this } // show fix-it ^ return *this; --- ret.cc +++ ret.cc @@ -1,5 +1,5 @@ struct X { - X& operator=(const X&) { } // show fix-it + X& operator=(const X&) { return *this;} // show fix-it }; struct Y { @@ -10,7 +10,7 @@ struct Z { Z& operator=(const Z& z) { i = z.i; - } // show fix-it + return *this;} // show fix-it int i; }; On IRC Dave said: to do it right, should handle both single-line and multiline methods and indent to match that of nearby code; see that add_newline_fixit_with_indentation from https://gcc.gnu.org/ml/gcc-patches/2017-05/msg00334.html probably a trailing space for the single-line case
Note to self: test coverage should also verify += and so on.
Note to self: this came out of this ML thread: https://gcc.gnu.org/ml/gcc/2018-04/msg00168.html
Author: dmalcolm Date: Tue May 1 00:10:10 2018 New Revision: 259783 URL: https://gcc.gnu.org/viewcvs?rev=259783&root=gcc&view=rev Log: Add gcc_rich_location::add_fixit_insert_formatted This patch adds a support function to class gcc_rich_location to make it easier for fix-it hints to use idiomatic C/C++ indentation, for use by the patch for PR c++/85523. gcc/ChangeLog: PR c++/85523 * gcc-rich-location.c (blank_line_before_p): New function. (use_new_line): New function. (gcc_rich_location::add_fixit_insert_formatted): New function. * gcc-rich-location.h (gcc_rich_location::add_fixit_insert_formatted): New function. gcc/testsuite/ChangeLog: PR c++/85523 * gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c (test_add_fixit_insert_formatted_single_line): New function. (test_add_fixit_insert_formatted_multiline): New function. Extend expected output of generated patch to include fix-it hints for these. * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c: Include "gcc-rich-location.h". Add test coverage for gcc_rich_location::add_fixit_insert_formatted. Modified: trunk/gcc/ChangeLog trunk/gcc/gcc-rich-location.c trunk/gcc/gcc-rich-location.h trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c trunk/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
Candidate patch: https://gcc.gnu.org/ml/gcc-patches/2018-05/msg00001.html
Author: dmalcolm Date: Fri Aug 3 18:38:13 2018 New Revision: 263298 URL: https://gcc.gnu.org/viewcvs?rev=263298&root=gcc&view=rev Log: Add fix-it hint for missing return statement in assignment operators (PR c++/85523) gcc/cp/ChangeLog: PR c++/85523 * decl.c: Include "gcc-rich-location.h". (add_return_star_this_fixit): New function. (finish_function): When warning about missing return statements in functions returning non-void, add a "return *this;" fix-it hint for assignment operators. gcc/testsuite/ChangeLog: PR c++/85523 * g++.dg/pr85523.C: New test. Added: trunk/gcc/testsuite/g++.dg/pr85523.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/decl.c trunk/gcc/testsuite/ChangeLog
Implemented in trunk (for gcc 9) by r263298.