Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-31910

Extract Function - refactoring does not respect possible modifications of elements in function

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • Qt Creator 15.0.0-beta1
    • C/C++/Obj-C++ Support
    • Windows

    Description

      The refactoring method: "Extract Function" does not respect possible modifications of members of the to be extracted code.

      #include <QDebug>
      #include <QVector>
      
      class A
         {
         public:
            int i() const;
            void setI(int newI);
      
         private:
            int _i;
         };
      
      int A::i() const
         {
         return _i;
         }
      
      void A::setI(int newI)
         {
         _i = newI;
         }
      
      int main()
         {
         A a1;
      
         // extract next three lines to a function.
         a1.setI(2);
         a1.setI(3);
         a1.setI(4);
      
         qDebug() << a1.i();
         }

      When extracting the next the marked lines in main() to an external function via the
      Refactor => Extract Function
      the resulting method looks like this:

      void modify(A a1)
         {
         a1.setI(2);
         a1.setI(3);
         a1.setI(4);
         }
      
      int main()
         {
         A a1;
      
         modify(a1);
      
         qDebug() << a1.i();
         } 

      As modify gets the parameter as copy the modification will be applied to the local a1.

      The a1 in the main() will not be modified.

       

      btw: using the refactoring "Clang: Extract to function" give a working result:

      void extracted(A &a1) {
         a1.setI(2);
         a1.setI(3);
         a1.setI(4);
         }
      int main() {
         A a1;
         
         extracted(a1);
         
         qDebug() << a1.i();
         }
       

      "extracted" gets a1 as reference and so the changes will be relevant in main().

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kandeler Christian Kandeler
            moellney Michael Möllney
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes