aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2023-06-12 15:55:36 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-08-30 15:54:00 +0000
commitc567489e73b244594f26e1d8babaa8801fadf7da (patch)
tree7ff98dd5d3c06f6396042afc17753f0eaba60156
parent9359056072285b7b5111767026df3751803d687f (diff)
qmllint: Detect unqualified id access in StoreNameSloppy
In QQmlJSTypePropagator::generate_StoreName, we would return early in a few error cases (after calling setError). That is okay for the compiler, which cares about setError, but qmllint is not interested in compiler errors by default. This would result in us not issuing warnings about unqualified id accesses. Fix this by also logging the errors via the logger. Fixes: QTBUG-114467 Change-Id: I10ad10e5c93762b3d9d1fb50d3cabcff2c0b7a4f Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> (cherry picked from commit 15082891f78d3e6df121fbaef7626be5250bd1e4) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/qmlcompiler/qqmljstypepropagator.cpp7
-rw-r--r--tests/auto/qml/qmllint/data/UnqualifiedInStoreSloppy.qml12
-rw-r--r--tests/auto/qml/qmllint/data/storeNameMethod.qml12
-rw-r--r--tests/auto/qml/qmllint/tst_qmllint.cpp7
4 files changed, 38 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmljstypepropagator.cpp b/src/qmlcompiler/qqmljstypepropagator.cpp
index 23668654d2..4ea5cedd1f 100644
--- a/src/qmlcompiler/qqmljstypepropagator.cpp
+++ b/src/qmlcompiler/qqmljstypepropagator.cpp
@@ -584,11 +584,18 @@ void QQmlJSTypePropagator::generate_StoreNameSloppy(int nameIndex)
const QQmlJSRegisterContent in = m_state.accumulatorIn();
if (!type.isValid()) {
+ handleUnqualifiedAccess(name, false);
setError(u"Cannot find name "_s + name);
return;
}
if (!type.isProperty()) {
+ QString message = type.isMethod() ? u"Cannot assign to method %1"_s
+ : u"Cannot assign to non-property %1"_s;
+ // The interpreter treats methods as read-only properties in its error messages
+ // and we lack a better fitting category. We might want to revisit this later.
+ m_logger->log(message.arg(name), qmlReadOnlyProperty,
+ getCurrentSourceLocation());
setError(u"Cannot assign to non-property "_s + name);
return;
}
diff --git a/tests/auto/qml/qmllint/data/UnqualifiedInStoreSloppy.qml b/tests/auto/qml/qmllint/data/UnqualifiedInStoreSloppy.qml
new file mode 100644
index 0000000000..197b74fa60
--- /dev/null
+++ b/tests/auto/qml/qmllint/data/UnqualifiedInStoreSloppy.qml
@@ -0,0 +1,12 @@
+import QtQuick 2.15
+import QtQuick.Window 2.15
+
+Window {
+ Rectangle {
+ property real divisor: 0
+
+ Timer {
+ onTriggered: divisor = 1
+ }
+ }
+}
diff --git a/tests/auto/qml/qmllint/data/storeNameMethod.qml b/tests/auto/qml/qmllint/data/storeNameMethod.qml
new file mode 100644
index 0000000000..fca02b288f
--- /dev/null
+++ b/tests/auto/qml/qmllint/data/storeNameMethod.qml
@@ -0,0 +1,12 @@
+import QtQuick 2.15
+import QtQuick.Window 2.15
+
+Window {
+ Rectangle {
+
+ Timer {
+ function foo() {}
+ onTriggered: foo = 1
+ }
+ }
+}
diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp
index 336a4f95bb..724d2d792d 100644
--- a/tests/auto/qml/qmllint/tst_qmllint.cpp
+++ b/tests/auto/qml/qmllint/tst_qmllint.cpp
@@ -263,6 +263,9 @@ void TestQmllint::testUnqualified_data()
Message {
QStringLiteral("index is implicitly injected into this delegate. "
"Add a required property instead.") } } };
+ QTest::newRow("storeSloppy")
+ << QStringLiteral("UnqualifiedInStoreSloppy.qml")
+ << Result{ { Message{ QStringLiteral("Unqualified access"), 9, 26} } };
}
void TestQmllint::testUnknownCausesFail()
@@ -1061,6 +1064,10 @@ expression: \${expr} \${expr} \\\${expr} \\\${expr}`)",
{},
{ Message{ QStringLiteral("Hours") } },
Result::ExitsNormally };
+
+ QTest::newRow("StoreNameMethod")
+ << QStringLiteral("storeNameMethod.qml")
+ << Result { { Message { QStringLiteral("Cannot assign to method foo") } } };
}
void TestQmllint::dirtyQmlCode()