From 1c60f57340bb2808d08169586e11e20ffcd0c987 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 19 Apr 2023 11:15:33 +0200 Subject: [PATCH] C++ editor: Make generated Q_PROPERTYs FINAL by default It's bad style to omit the FINAL because such properties can be shadowed, causing problems in QML. Change-Id: I9083c69128f6335f584f0a1d28f1fe1e54a02eaf Reviewed-by: Christian Kandeler Reviewed-by: Jarek Kobus --- share/qtcreator/snippets/cpp.xml | 2 +- src/plugins/cppeditor/cppquickfix_test.cpp | 22 ++++++++++----------- src/plugins/cppeditor/cppquickfixes.cpp | 2 +- src/plugins/texteditor/snippets/snippet.cpp | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/share/qtcreator/snippets/cpp.xml b/share/qtcreator/snippets/cpp.xml index 82a62b36e6a..ca62a37998a 100644 --- a/share/qtcreator/snippets/cpp.xml +++ b/share/qtcreator/snippets/cpp.xml @@ -204,5 +204,5 @@ case $value$: default: break; } -Q_PROPERTY($type$ $name$ READ $name$ WRITE set$name:c$ NOTIFY $name$Changed) +Q_PROPERTY($type$ $name$ READ $name$ WRITE set$name:c$ NOTIFY $name$Changed FINAL) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 560a27fe240..9c2c011dc5a 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -2315,7 +2315,7 @@ signals: void newFooBarTestValue(); private: - Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue) + Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue FINAL) }; )-"; QTest::addRow("create right names") << QByteArrayList{originalSource, expectedSource} << 4; @@ -2346,7 +2346,7 @@ signals: void newFooBarTestValue(); private: - Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue) + Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue FINAL) }; )-"; expectedSource = ""; @@ -2355,7 +2355,7 @@ private: // create from Q_PROPERTY with custom names originalSource = R"-( class Test { - Q_PROPER@TY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue) + Q_PROPER@TY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue FINAL) public: int give_me_foo_bar_test() const @@ -2380,7 +2380,7 @@ signals: )-"; expectedSource = R"-( class Test { - Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue) + Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue FINAL) public: int give_me_foo_bar_test() const @@ -2411,14 +2411,14 @@ private: // create from Q_PROPERTY with custom names originalSource = R"-( class Test { - Q_PROPE@RTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue) + Q_PROPE@RTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue FINAL) int mem_fooBar_test; public: }; )-"; expectedSource = R"-( class Test { - Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue) + Q_PROPERTY(int fooBar_test READ give_me_foo_bar_test WRITE Seet_FooBar_test RESET set_fooBarTest_toDefault NOTIFY newFooBarTestValue FINAL) int mem_fooBar_test; public: int give_me_foo_bar_test() const @@ -2766,7 +2766,7 @@ public: signals: void barChanged(N2::test *bar); private: - Q_PROPERTY(N2::test *bar READ getBar NOTIFY barChanged) + Q_PROPERTY(N2::test *bar READ getBar NOTIFY barChanged FINAL) }; })--"; testDocuments << CppTestDocument::create("file.h", original, expected); @@ -3303,7 +3303,7 @@ void QuickfixTest::testGenerateGetterSetterAnonymousClass() void fooChanged(); private: - Q_PROPERTY(int foo READ foo WRITE setFoo RESET resetFoo NOTIFY fooChanged) + Q_PROPERTY(int foo READ foo WRITE setFoo RESET resetFoo NOTIFY fooChanged FINAL) } bar; )"; testDocuments << CppTestDocument::create("file.h", original, expected); @@ -3334,7 +3334,7 @@ public: signals: void barChanged(); private: - Q_PROPERTY(int bar READ getBar WRITE setBar RESET resetBar NOTIFY barChanged) + Q_PROPERTY(int bar READ getBar WRITE setBar RESET resetBar NOTIFY barChanged FINAL) }; inline int Foo::getBar() const @@ -3560,8 +3560,8 @@ private: int m_bar; int bar2_; QString bar3; - Q_PROPERTY(int bar2 READ getBar2 WRITE setBar2 RESET resetBar2 NOTIFY bar2Changed) - Q_PROPERTY(QString bar3 READ getBar3 WRITE setBar3 RESET resetBar3 NOTIFY bar3Changed) + Q_PROPERTY(int bar2 READ getBar2 WRITE setBar2 RESET resetBar2 NOTIFY bar2Changed FINAL) + Q_PROPERTY(QString bar3 READ getBar3 WRITE setBar3 RESET resetBar3 NOTIFY bar3Changed FINAL) }; inline void Foo::resetBar() { diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index dad99ff1344..1c76f8f2c57 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -4182,7 +4182,7 @@ void GetterSetterRefactoringHelper::performGeneration(ExistingGetterSetterData d propertyDeclaration.append(QLatin1String(" NOTIFY ")).append(data.signalName); } - propertyDeclaration.append(QLatin1String(")\n")); + propertyDeclaration.append(QLatin1String(" FINAL)\n")); addHeaderCode(InsertionPointLocator::Private, propertyDeclaration); } } diff --git a/src/plugins/texteditor/snippets/snippet.cpp b/src/plugins/texteditor/snippets/snippet.cpp index f41156c4cee..74a4dc543c3 100644 --- a/src/plugins/texteditor/snippets/snippet.cpp +++ b/src/plugins/texteditor/snippets/snippet.cpp @@ -330,7 +330,7 @@ void Internal::TextEditorPlugin::testSnippetParsing_data() << QString::fromLatin1("\\\\$test\\\\\\\\$\\\\") << false << Parts(); QTest::newRow("Q_PROPERTY") << QString( - "Q_PROPERTY($type$ $name$ READ $name$ WRITE set$name:c$ NOTIFY $name$Changed)") + "Q_PROPERTY($type$ $name$ READ $name$ WRITE set$name:c$ NOTIFY $name$Changed FINAL)") << true << Parts{SnippetPart("Q_PROPERTY("), SnippetPart("type", 0), @@ -342,7 +342,7 @@ void Internal::TextEditorPlugin::testSnippetParsing_data() SnippetPart("name", 1, TCMANGLER_ID), SnippetPart(" NOTIFY "), SnippetPart("name", 1), - SnippetPart("Changed)")}; + SnippetPart("Changed FINAL)")}; QTest::newRow("open identifier") << QString("$test") << false << Parts(); QTest::newRow("wrong mangler") << QString("$test:X$") << false << Parts();