diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h
index b5b110feee72e96aa9a1e5e21d300c06248878ea..40787b91dd5eaa212843226bc520ed8124ab84a5 100644
--- a/src/plugins/cppeditor/cppeditorplugin.h
+++ b/src/plugins/cppeditor/cppeditorplugin.h
@@ -141,6 +141,13 @@ private slots:
     void test_quickfix_GenerateGetterSetter_notTriggeringOnMemberArray();
     void test_quickfix_GenerateGetterSetter_notTriggeringWhenGetterOrSetterExist();
 
+    void test_quickfix_MoveDeclarationOutOfIf_ifOnly();
+    void test_quickfix_MoveDeclarationOutOfIf_ifElse();
+    void test_quickfix_MoveDeclarationOutOfIf_ifElseIf();
+
+    void test_quickfix_MoveDeclarationOutOfWhile_singleWhile();
+    void test_quickfix_MoveDeclarationOutOfWhile_whileInWhile();
+
     void test_quickfix_ReformatPointerDeclaration();
 
     void test_quickfix_InsertDefFromDecl_basic();
diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp
index 55fad435e7db96c1d4272e991be911a243b50b16..914240dda7a5c5b1636e019199b6f4182f755a3e 100644
--- a/src/plugins/cppeditor/cppquickfix_test.cpp
+++ b/src/plugins/cppeditor/cppquickfix_test.cpp
@@ -1003,6 +1003,145 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_notTriggeringWhenGetter
     data.run(&factory);
 }
 
+void CppEditorPlugin::test_quickfix_MoveDeclarationOutOfIf_ifOnly()
+{
+    const QByteArray original =
+            "void f()\n"
+            "{\n"
+            "    if (Foo *@foo = g())\n"
+            "        h();\n"
+            "}\n";
+
+    const QByteArray expected =
+            "void f()\n"
+            "{\n"
+            "    Foo *foo = g();\n"
+            "    if (foo)\n"
+            "        h();\n"
+            "}\n"
+            "\n";
+
+    MoveDeclarationOutOfIf factory;
+    TestCase data(original, expected);
+    data.run(&factory);
+}
+
+void CppEditorPlugin::test_quickfix_MoveDeclarationOutOfIf_ifElse()
+{
+    const QByteArray original =
+            "void f()\n"
+            "{\n"
+            "    if (Foo *@foo = g())\n"
+            "        h();\n"
+            "    else\n"
+            "        i();\n"
+            "}\n";
+
+    const QByteArray expected =
+            "void f()\n"
+            "{\n"
+            "    Foo *foo = g();\n"
+            "    if (foo)\n"
+            "        h();\n"
+            "    else\n"
+            "        i();\n"
+            "}\n"
+            "\n";
+
+    MoveDeclarationOutOfIf factory;
+    TestCase data(original, expected);
+    data.run(&factory);
+}
+
+void CppEditorPlugin::test_quickfix_MoveDeclarationOutOfIf_ifElseIf()
+{
+    const QByteArray original =
+            "void f()\n"
+            "{\n"
+            "    if (Foo *foo = g()) {\n"
+            "        if (Bar *@bar = x()) {\n"
+            "            h();\n"
+            "            j();\n"
+            "        }\n"
+            "    } else {\n"
+            "        i();\n"
+            "    }\n"
+            "}\n";
+
+    const QByteArray expected =
+            "void f()\n"
+            "{\n"
+            "    if (Foo *foo = g()) {\n"
+            "        Bar *bar = x();\n"
+            "        if (bar) {\n"
+            "            h();\n"
+            "            j();\n"
+            "        }\n"
+            "    } else {\n"
+            "        i();\n"
+            "    }\n"
+            "}\n"
+            "\n";
+
+    MoveDeclarationOutOfIf factory;
+    TestCase data(original, expected);
+    data.run(&factory);
+}
+
+void CppEditorPlugin::test_quickfix_MoveDeclarationOutOfWhile_singleWhile()
+{
+    const QByteArray original =
+            "void f()\n"
+            "{\n"
+            "    while (Foo *@foo = g())\n"
+            "        j();\n"
+            "}\n";
+
+    const QByteArray expected =
+            "void f()\n"
+            "{\n"
+            "    Foo *foo;\n"
+            "    while ((foo = g()) != 0)\n"
+            "        j();\n"
+            "}\n"
+            "\n";
+
+    MoveDeclarationOutOfWhile factory;
+    TestCase data(original, expected);
+    data.run(&factory);
+}
+
+void CppEditorPlugin::test_quickfix_MoveDeclarationOutOfWhile_whileInWhile()
+{
+    const QByteArray original =
+            "void f()\n"
+            "{\n"
+            "    while (Foo *foo = g()) {\n"
+            "        while (Bar *@bar = h()) {\n"
+            "            i();\n"
+            "            j();\n"
+            "        }\n"
+            "    }\n"
+            "}\n";
+
+    const QByteArray expected =
+            "void f()\n"
+            "{\n"
+            "    while (Foo *foo = g()) {\n"
+            "        Bar *bar;\n"
+            "        while ((bar = h()) != 0) {\n"
+            "            i();\n"
+            "            j();\n"
+            "        }\n"
+            "    }\n"
+            "}\n"
+            "\n";
+
+    MoveDeclarationOutOfWhile factory;
+    TestCase data(original, expected);
+    data.run(&factory);
+}
+
 /// Check: Just a basic test since the main functionality is tested in
 /// cpppointerdeclarationformatter_test.cpp
 void CppEditorPlugin::test_quickfix_ReformatPointerDeclaration()