diff options
| author | Ulf Hermann <ulf.hermann@qt.io> | 2026-03-12 14:41:50 +0100 |
|---|---|---|
| committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2026-03-14 06:34:21 +0000 |
| commit | da768d5c3795942a2f14130225dea5881a40c443 (patch) | |
| tree | 861fb05c6bd2f504726e1e547dfcad8fb1ea7780 | |
| parent | 5d5825f097042b52c4e6007148d364608105a460 (diff) | |
QmlCompiler: Don't warn for ValueTypeBehavior: Assertable
We don't use the attribute so far, but we should certainly welcome its
usage.
Pick-to: 6.8
Fixes: QTBUG-144585
Change-Id: Iaa69c9401d411a4ad986d4c46ace0bad049b9881
Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io>
(cherry picked from commit 9c8a2dab276a10064bd4ac608b5ba886fc492558)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 0fe39fca4edee70b90726090bfe0d986b834ba07)
| -rw-r--r-- | src/qmlcompiler/qqmljsimportvisitor.cpp | 4 | ||||
| -rw-r--r-- | src/qmlcompiler/qqmljsscopesbyid_p.h | 4 | ||||
| -rw-r--r-- | tests/auto/qml/qmllint/data/asCast.qml | 41 | ||||
| -rw-r--r-- | tests/auto/qml/qmllint/data/asValueTypeGood.qml | 32 | ||||
| -rw-r--r-- | tests/auto/qml/qmllint/tst_qmllint.cpp | 2 |
5 files changed, 83 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmljsimportvisitor.cpp b/src/qmlcompiler/qqmljsimportvisitor.cpp index 86f38bd9d7..4552960b19 100644 --- a/src/qmlcompiler/qqmljsimportvisitor.cpp +++ b/src/qmlcompiler/qqmljsimportvisitor.cpp @@ -2917,6 +2917,10 @@ bool QQmlJSImportVisitor::visit(QQmlJS::AST::UiPragma *pragma) m_scopesById.setValueTypesAreAddressable(true); } else if (value == u"Inaddressable") { m_scopesById.setValueTypesAreAddressable(false); + } else if (value == u"Assertable") { + m_scopesById.setValueTypesAreAssertable(true); + } else if (value == u"Inassertable") { + m_scopesById.setValueTypesAreAssertable(false); } else { m_logger->log(u"Unknown argument \"%1\" to pragma ValueTypeBehavior"_s.arg(value), qmlSyntax, pragma->firstSourceLocation()); diff --git a/src/qmlcompiler/qqmljsscopesbyid_p.h b/src/qmlcompiler/qqmljsscopesbyid_p.h index 2f17f0c95d..b28dd8fb99 100644 --- a/src/qmlcompiler/qqmljsscopesbyid_p.h +++ b/src/qmlcompiler/qqmljsscopesbyid_p.h @@ -78,6 +78,9 @@ public: void setValueTypesAreAddressable(bool addressable) { m_valueTypesAreAddressable = addressable; } bool valueTypesAreAddressable() const { return m_valueTypesAreAddressable; } + void setValueTypesAreAssertable(bool assertable) { m_valueTypesAreAssertable = assertable; } + bool valueTypesAreAssertable() const { return m_valueTypesAreAssertable; } + /*! \internal Find the possible IDs for \a scope as seen by \a referrer. There can be at most one @@ -315,6 +318,7 @@ private: bool m_componentsAreBound = false; bool m_signaturesAreEnforced = true; bool m_valueTypesAreAddressable = false; + bool m_valueTypesAreAssertable = false; }; QT_END_NAMESPACE diff --git a/tests/auto/qml/qmllint/data/asCast.qml b/tests/auto/qml/qmllint/data/asCast.qml new file mode 100644 index 0000000000..c6201aa6f8 --- /dev/null +++ b/tests/auto/qml/qmllint/data/asCast.qml @@ -0,0 +1,41 @@ +// Copyright (C) 2021 The Qt Company Ltd. + +pragma Strict +pragma ValueTypeBehavior: Assertable + +import QtQuick +import QtQml as QQ + +Item { + QtObject { id: object } + Item { id: item } + Rectangle { id: rectangle } + + property string string: "a" + property url url: "http://example.com" + property date date: new Date(1996, 2, 3) + + property QtObject objectAsObject: object as QtObject + property QtObject objectAsItem: object as Item + property QtObject objectAsRectangle: object as Rectangle + + property QtObject itemAsObject: item as QtObject + property QtObject itemAsItem: item as Item + property QtObject itemAsRectangle: item as Rectangle + + property QtObject rectangleAsObject: rectangle as QtObject + property QtObject rectangleAsItem: rectangle as Item + property QtObject rectangleAsRectangle: rectangle as Rectangle + + property QtObject nullAsObject: null as QtObject + property QtObject nullAsItem: null as Item + property QtObject nullAsRectangle: null as Rectangle + + property QtObject undefinedAsObject: undefined as QtObject + property QtObject undefinedAsItem: undefined as Item + property QtObject undefinedAsRectangle: undefined as Rectangle + + property var stringAsString: string as QQ.string + property var urlAsUrl: url as QQ.url + property var dateAsDate: date as QQ.date +} diff --git a/tests/auto/qml/qmllint/data/asValueTypeGood.qml b/tests/auto/qml/qmllint/data/asValueTypeGood.qml new file mode 100644 index 0000000000..c5441ca09d --- /dev/null +++ b/tests/auto/qml/qmllint/data/asValueTypeGood.qml @@ -0,0 +1,32 @@ +pragma ValueTypeBehavior: Assertable +import QtQml as Q + +Q.QtObject { + property var a + property rect b: a as Q.rect + property bool c: a instanceof Q.rect + property bool d: ({x: 10, y: 20}) instanceof Q.point + property var e: ({x: 10, y: 20}) as Q.point + property var g: "green" as Q.string + + property var i: { + let p = new Q.point; + p.x = 10 + p.y = 20 + return p + } + + property var j: 4.0 as Q.int + property var k: (4.5 / 1.5) as Q.int + property var l: 5 as Q.double + property var m: "something" as Q.var + property var n: 1 as Q.bool + property var o: Infinity as Q.int + + property var p: b as Q.size; + property var q: this as Q.size; + property var r: ({}) as Q.size; + property var s: 11 as Q.size; + property var t: Q.Component as Q.size; + property var u: Q.Qt as Q.size; +} diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp index b9ab17d35a..aef02664fa 100644 --- a/tests/auto/qml/qmllint/tst_qmllint.cpp +++ b/tests/auto/qml/qmllint/tst_qmllint.cpp @@ -2193,6 +2193,8 @@ void TestQmllint::cleanQmlCode_data() << QStringLiteral("aliasToRequiredPropertyIsNotRequiredItself.qml"); QTest::newRow("anchors1") << QStringLiteral("anchors1.qml"); QTest::newRow("anchors2") << QStringLiteral("anchors2.qml"); + QTest::newRow("asCast") << QStringLiteral("asCast.qml"); + QTest::newRow("asValueTypeGood") << QStringLiteral("asValueTypeGood.qml"); QTest::newRow("attached") << QStringLiteral("attached.qml"); QTest::newRow("attachedImportUse") << QStringLiteral("attachedImportUse.qml"); QTest::newRow("attachedPropertyAssignments") |
