diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index f84d6828548..37c9229ed58 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -154,6 +154,7 @@ private slots: void test_quickfix_InsertDefFromDecl_macroUsesAtEndOfFile2(); void test_quickfix_InsertDefFromDecl_erroneousStatementAtEndOfFile(); void test_quickfix_InsertDefFromDecl_rvalueReference(); + void test_quickfix_InsertDefFromDecl_functionTryBlock(); void test_quickfix_InsertDefFromDecl_findImplementationFile(); void test_quickfix_InsertDefFromDecl_unicodeIdentifier(); void test_quickfix_InsertDefFromDecl_templateClass(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 031ab3fe2dd..757659911bb 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -4283,6 +4283,45 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_rvalueReference() QuickFixOperationTest(testDocuments, &factory); } +void CppEditorPlugin::test_quickfix_InsertDefFromDecl_functionTryBlock() +{ + QList testDocuments; + + QByteArray original; + QByteArray expected; + + // Header File + original = R"( +struct Foo { + void tryCatchFunc(); + void @otherFunc(); +}; +)"; + expected = original; + testDocuments << QuickFixTestDocument::create("file.h", original, expected); + + // Source File + original = R"( +#include "file.h" + +void Foo::tryCatchFunc() try {} catch (...) {} +)"; + expected = R"( +#include "file.h" + +void Foo::tryCatchFunc() try {} catch (...) {} + +void Foo::otherFunc() +{ + +} +)"; + testDocuments << QuickFixTestDocument::create("file.cpp", original, expected); + + InsertDefFromDecl factory; + QuickFixOperationTest(testDocuments, &factory); +} + /// Find right implementation file. (QTCREATORBUG-10728) void CppEditorPlugin::test_quickfix_InsertDefFromDecl_findImplementationFile() { diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 35e75bc9c96..db6f28aa987 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -3111,6 +3111,15 @@ public: // make target lookup context Document::Ptr targetDoc = targetFile->cppDocument(); Scope *targetScope = targetDoc->scopeAt(loc.line(), loc.column()); + + // Correct scope in case of a function try-block. See QTCREATORBUG-14661. + if (targetScope && targetScope->asBlock()) { + if (Class * const enclosingClass = targetScope->enclosingClass()) + targetScope = enclosingClass; + else + targetScope = targetScope->enclosingNamespace(); + } + LookupContext targetContext(targetDoc, op->snapshot()); ClassOrNamespace *targetCoN = targetContext.lookupType(targetScope); if (!targetCoN)