diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index d8714bb99c960294bac2c0743beed1fe205b12de..40c8b1ac48af1ee9602eec9885aafa4252561df9 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -157,6 +157,7 @@ private slots: void test_quickfix_InsertDefFromDecl_headerSource_namespace2(); void test_quickfix_InsertDefFromDecl_freeFunction(); void test_quickfix_InsertDefFromDecl_insideClass(); + void test_quickfix_InsertDefFromDecl_notTriggeringWhenDefinitionExists(); void test_quickfix_InsertDeclFromDef(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 35423c5b54811f0a37691dcbfb38f27e0fde6248..25059217b59ad20b9b6652e0557885f7076cbe68 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -893,6 +893,21 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_insideClass() data.run(&factory, 1); } +/// Check not triggering when definition exists +void CppEditorPlugin::test_quickfix_InsertDefFromDecl_notTriggeringWhenDefinitionExists() +{ + const QByteArray original = + "class Foo {\n" + " void b@ar();\n" + "};\n" + "void Foo::bar() {}\n"; + const QByteArray expected = original + "\n"; + + InsertDefFromDecl factory; + TestCase data(original, expected); + data.run(&factory, 1); +} + // Function for one of InsertDeclDef section cases void insertToSectionDeclFromDef(const QByteArray §ion, int sectionIndex) { diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 0b42f5bfbb8a552834a9fbbce8447cc8d97b36d3..5d67df6037b15ab7690855756ef6145e5d299f25 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -2621,6 +2621,13 @@ void InsertDefFromDecl::match(const CppQuickFixInterface &interface, QuickFixOpe const QString cppFileName = correspondingHeaderOrSource( interface->fileName(), &isHeaderFile); + // Check if there is already a definition + CppTools::SymbolFinder symbolFinder; + if (symbolFinder.findMatchingDefinition(decl, interface->snapshot(), + true)) { + return; + } + // Insert Position: Implementation File if (isHeaderFile && !cppFileName.isEmpty()) { CppRefactoringChanges refactoring(interface->snapshot());