From e3d95168a08c36a8cca4f08d906d3a6162eff9f5 Mon Sep 17 00:00:00 2001
From: Lorenz Haas <lykurg@gmail.com>
Date: Sat, 5 Oct 2013 21:15:43 +0200
Subject: [PATCH] CppEditor: Fix insert position in MoveFuncDefToDecl

When a class was directly assigned to a variable the definition was
misplaced right after the variable.

Task-number: QTCREATORBUG-10303
Change-Id: I2cdfee784b085d856d7ff5ebe62bf791b9a6754e
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
---
 src/plugins/cppeditor/cppeditorplugin.h    |  1 +
 src/plugins/cppeditor/cppquickfix_test.cpp | 27 ++++++++++++++++++++++
 src/plugins/cppeditor/cppquickfixes.cpp    |  2 +-
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h
index 99b0bdc811d..954df14b5d7 100644
--- a/src/plugins/cppeditor/cppeditorplugin.h
+++ b/src/plugins/cppeditor/cppeditorplugin.h
@@ -238,6 +238,7 @@ private slots:
     void test_quickfix_MoveFuncDefToDecl_FreeFuncToCpp();
     void test_quickfix_MoveFuncDefToDecl_FreeFuncToCppNS();
     void test_quickfix_MoveFuncDefToDecl_CtorWithInitialization();
+    void test_quickfix_MoveFuncDefToDecl_structWithAssignedVariable();
 
     void test_quickfix_AssignToLocalVariable_freeFunction();
     void test_quickfix_AssignToLocalVariable_memberFunction();
diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp
index 7c4051d389e..53c74826fd5 100644
--- a/src/plugins/cppeditor/cppquickfix_test.cpp
+++ b/src/plugins/cppeditor/cppquickfix_test.cpp
@@ -3047,6 +3047,33 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_CtorWithInitialization()
     data.run(&factory);
 }
 
+/// Check: Definition should not be placed behind the variable. QTCREATORBUG-10303
+void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_structWithAssignedVariable()
+{
+    QByteArray original =
+        "struct Foo\n"
+        "{\n"
+        "    void foo();\n"
+        "} bar;\n\n"
+        "void Foo::fo@o()\n"
+        "{\n"
+        "    return;\n"
+        "}";
+
+    QByteArray expected =
+        "struct Foo\n"
+        "{\n"
+        "    void foo()\n"
+        "    {\n"
+        "        return;\n"
+        "    }\n"
+        "} bar;\n\n\n";
+
+    MoveFuncDefToDecl factory;
+    TestCase data(original, expected);
+    data.run(&factory);
+}
+
 /// Check: Add local variable for a free function.
 void CppEditorPlugin::test_quickfix_AssignToLocalVariable_freeFunction()
 {
diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp
index a6d9dd1900b..4802a3a3464 100644
--- a/src/plugins/cppeditor/cppquickfixes.cpp
+++ b/src/plugins/cppeditor/cppquickfixes.cpp
@@ -4387,7 +4387,7 @@ void MoveFuncDefToDecl::match(const CppQuickFixInterface &interface, QuickFixOpe
             const CppRefactoringFilePtr declFile = refactoring.file(declFileName);
             ASTPath astPath(declFile->cppDocument());
             const QList<AST *> path = astPath(s->line(), s->column());
-            for (int idx = 0; idx < path.size(); ++idx) {
+            for (int idx = path.size() - 1; idx > 0; --idx) {
                 AST *node = path.at(idx);
                 if (SimpleDeclarationAST *simpleDecl = node->asSimpleDeclaration()) {
                     if (simpleDecl->symbols && !simpleDecl->symbols->next) {
-- 
GitLab