This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/85523] Add fix-it hint for missing return statement in assignment operators


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2018-04-25
     Ever confirmed|0                           |1

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]