Bug 85523 - Add fix-it hint for missing return statement in assignment operators
Summary: Add fix-it hint for missing return statement in assignment operators
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 enhancement
Target Milestone: 9.0
Assignee: David Malcolm
URL:
Keywords: diagnostic, patch
Depends on:
Blocks:
 
Reported: 2018-04-25 17:38 UTC by Jonathan Wakely
Modified: 2021-05-03 20:40 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-04-25 00:00:00


Attachments
Proof-of-concept patch to add fix-it (623 bytes, patch)
2018-04-25 17:39 UTC, Jonathan Wakely
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2018-04-25 17:38:24 UTC
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;
};
Comment 1 Jonathan Wakely 2018-04-25 17:39:22 UTC
Created attachment 44017 [details]
Proof-of-concept patch to add fix-it
Comment 2 Jonathan Wakely 2018-04-25 17:41:40 UTC
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
Comment 3 David Malcolm 2018-04-25 18:03:46 UTC
Note to self: test coverage should also verify += and so on.
Comment 4 David Malcolm 2018-04-25 18:05:21 UTC
Note to self: this came out of this ML thread:
  https://gcc.gnu.org/ml/gcc/2018-04/msg00168.html
Comment 5 David Malcolm 2018-05-01 00:10:42 UTC
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
Comment 6 David Malcolm 2018-05-01 00:23:47 UTC
Candidate patch:
  https://gcc.gnu.org/ml/gcc-patches/2018-05/msg00001.html
Comment 7 David Malcolm 2018-08-03 18:38:45 UTC
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
Comment 8 David Malcolm 2018-08-03 18:44:15 UTC
Implemented in trunk (for gcc 9) by r263298.