diff options
author | Olivier De Cannière <[email protected]> | 2025-04-25 18:54:39 +0200 |
---|---|---|
committer | Olivier De Cannière <[email protected]> | 2025-04-29 09:46:49 +0200 |
commit | 959836a5b3e2cc106ed8c05649c81ed873aa12e2 (patch) | |
tree | 28713cd6cbdfb57a4230a3a76d1bb7f0e20b8ab9 /tests | |
parent | 3f876d603378d6fd6e2ec5524a54060121cc6cdd (diff) |
qmllint: Warn about reads of non-constant and non-notifiable properties
The binding might not update if their value changes.
Fixes: QTBUG-112508
Change-Id: I27801d662117a89c0fdddc2aaa2f1dde21b238df
Reviewed-by: Sami Shalayel <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qmllint/data/StalePropertyRead.qml | 19 | ||||
-rw-r--r-- | tests/auto/qml/qmllint/data/TestTypes/testtypes.qmltypes | 14 | ||||
-rw-r--r-- | tests/auto/qml/qmllint/tst_qmllint.cpp | 12 |
3 files changed, 45 insertions, 0 deletions
diff --git a/tests/auto/qml/qmllint/data/StalePropertyRead.qml b/tests/auto/qml/qmllint/data/StalePropertyRead.qml new file mode 100644 index 0000000000..32b9fa6347 --- /dev/null +++ b/tests/auto/qml/qmllint/data/StalePropertyRead.qml @@ -0,0 +1,19 @@ +import QtQml +import TestTypes + +StaleBindingPropertyRead { + id: o + property int i: 0 + readonly property int ro: 0 + + // Warn + property int p1: o.cppStale + property int p2: o.cppReadonly + + // No Warn + property int p3: o.cppConstant + property int p4: o.cppNotifiable + property int p5: o.cppConstantNotifiable + property int p6: o.i + property int p7: o.ro +} diff --git a/tests/auto/qml/qmllint/data/TestTypes/testtypes.qmltypes b/tests/auto/qml/qmllint/data/TestTypes/testtypes.qmltypes index 77ad6a3cef..7aeb8f5507 100644 --- a/tests/auto/qml/qmllint/data/TestTypes/testtypes.qmltypes +++ b/tests/auto/qml/qmllint/data/TestTypes/testtypes.qmltypes @@ -51,6 +51,7 @@ Module { bindable: "rsvpBindable" read: "rsvp" write: "setRsvp" + notify: "rsvpChanged" index: 0 } } @@ -188,6 +189,7 @@ Module { name: "data" type: "QVariantMap" read: "data" + notify: "dataChanged" index: 0 } } @@ -198,4 +200,16 @@ Module { exports: ["TestTypes/Foo 1.0"] Method { name: "print" } } + Component { + file: "testtypes.h" + name: "StaleBindingPropertyRead" + accessSemantics: "reference" + prototype: "QObject" + exports: ["StaticTest/StaleBindingPropertyRead 1.0"] + Property { name: "cppStale"; type: "int"; index: 1 } + Property { name: "cppConstant"; type: "int"; isPropertyConstant: true; index: 2 } + Property { name: "cppNotifiable"; type: "int"; notify: "cppNotifiableChanged"; index: 3 } + Property { name: "cppConstantNotifiable"; type: "int"; isPropertyConstant: true; notify: "cppConstantNotifiableChanged"; index: 4 } + Property { name: "cppReadonly"; type: "int"; isReadonly: true; index: 5 } + } } diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp index bb7d985d03..aade1e8f46 100644 --- a/tests/auto/qml/qmllint/tst_qmllint.cpp +++ b/tests/auto/qml/qmllint/tst_qmllint.cpp @@ -1324,6 +1324,18 @@ expression: \${expr} \${expr} \\\${expr} \\\${expr}`)", << QStringLiteral("RedundantOptionalChainingEnums.qml") << Result{ { { "Redundant optional chaining for enum lookup"_L1, 5, 54 }, { "Redundant optional chaining for enum lookup"_L1, 6, 26 } } }; + { + const auto msgGen = [](const QString &name, quint32 line, quint32 col) { + return Message{ "Reading non-constant and non-notifiable property %1. Binding might "_L1 + "not update when the property changes."_L1.arg(name), line, col }; + }; + QTest::newRow("stalePropertyRead") + << QStringLiteral("StalePropertyRead.qml") + << Result{ { msgGen("cppStale"_L1, 10, 24), msgGen("cppReadonly"_L1, 11, 24) }, + { msgGen("cppConstant"_L1, 14, 24), msgGen("cppNotifiable"_L1, 15, 24), + msgGen("cppConstantNotifiable"_L1, 16, 24), msgGen("i"_L1, 17, 24), + msgGen("ro"_L1, 18, 24) } }; + } } void TestQmllint::dirtyQmlCode() |