From 359bc13ea0f45ba0362238720b35ad38c373baa7 Mon Sep 17 00:00:00 2001
From: Nikolai Kosjar <nikolai.kosjar@digia.com>
Date: Wed, 23 Jul 2014 14:21:44 +0200
Subject: [PATCH] CppEditor: Fix "Complete Switch Statement"

...if enum type is defined inside a class or namespace.

Task-number: QTCREATORBUG-12311
Change-Id: I71b64bbe9d419707b66caacd10550041efc1520c
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
---
 src/plugins/cppeditor/cppquickfix_test.cpp | 44 ++++++++++++++++++++++
 src/plugins/cppeditor/cppquickfixes.cpp    |  2 +
 2 files changed, 46 insertions(+)

diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp
index a1f08a02f14..3e28a2025a8 100644
--- a/src/plugins/cppeditor/cppquickfix_test.cpp
+++ b/src/plugins/cppeditor/cppquickfix_test.cpp
@@ -325,6 +325,50 @@ void CppEditorPlugin::test_quickfix_data()
         "}\n"
     );
 
+    // Checks: Enum type in class is found.
+    QTest::newRow("CompleteSwitchCaseStatement_enumTypeInClass")
+        << CppQuickFixFactoryPtr(new CompleteSwitchCaseStatement) << _(
+        "struct C { enum EnumType { V1, V2 }; };\n"
+        "\n"
+        "void f(C::EnumType t) {\n"
+        "    @switch (t) {\n"
+        "    }\n"
+        "}\n"
+        ) << _(
+        "struct C { enum EnumType { V1, V2 }; };\n"
+        "\n"
+        "void f(C::EnumType t) {\n"
+        "    switch (t) {\n"
+        "    case C::V1:\n"
+        "        break;\n"
+        "    case C::V2:\n"
+        "        break;\n"
+        "    }\n"
+        "}\n"
+    );
+
+    // Checks: Enum type in namespace is found.
+    QTest::newRow("CompleteSwitchCaseStatement_enumTypeInNamespace")
+        << CppQuickFixFactoryPtr(new CompleteSwitchCaseStatement) << _(
+        "namespace N { enum EnumType { V1, V2 }; };\n"
+        "\n"
+        "void f(N::EnumType t) {\n"
+        "    @switch (t) {\n"
+        "    }\n"
+        "}\n"
+        ) << _(
+        "namespace N { enum EnumType { V1, V2 }; };\n"
+        "\n"
+        "void f(N::EnumType t) {\n"
+        "    switch (t) {\n"
+        "    case N::V1:\n"
+        "        break;\n"
+        "    case N::V2:\n"
+        "        break;\n"
+        "    }\n"
+        "}\n"
+    );
+
     // Checks: The missing enum value is added.
     QTest::newRow("CompleteSwitchCaseStatement_oneValueMissing")
         << CppQuickFixFactoryPtr(new CompleteSwitchCaseStatement) << _(
diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp
index be1b3483409..dfdcfbc18f2 100644
--- a/src/plugins/cppeditor/cppquickfixes.cpp
+++ b/src/plugins/cppeditor/cppquickfixes.cpp
@@ -2293,6 +2293,8 @@ Enum *findEnum(const QList<LookupItem> &results, const LookupContext &ctxt)
             if (ClassOrNamespace *con = ctxt.lookupType(namedType->name(), result.scope())) {
                 const QList<Enum *> enums = con->unscopedEnums();
                 const Name *referenceName = namedType->name();
+                if (const QualifiedNameId *qualifiedName = referenceName->asQualifiedNameId())
+                    referenceName = qualifiedName->name();
                 foreach (Enum *e, enums) {
                     if (const Name *candidateName = e->name()) {
                         if (candidateName->match(referenceName))
-- 
GitLab