From 59d6cf324dfb80febf65dd735990b4e9e3a7e54b Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Wed, 4 Jan 2023 14:35:17 +0100 Subject: [PATCH] qmllint: Do not warn when accessing properties of global constructors We are treating global constructor functions as methods. But while they are callable, they also have properties that can be accessed. Accessing those should not trigger any warning. However, prior to this commit, any property access to a method would yield a warning. As we don't store the list of known properties in the jsroot.qmltypes, we cannot really validate them. Moreover, it is also possilbe to extend the prototypes, so it might never be feasible to generate warnings. Thus, for now simply don't create warnings for JS globals in QQmlJSTypePropagator::isRestricted. Fixes: QTBUG-109204 Pick-to: 6.5 6.4 Change-Id: I992365ea716827a562886d7204b2401062772f9a Reviewed-by: Ulf Hermann Reviewed-by: Qt CI Bot --- src/qmlcompiler/qqmljstypepropagator.cpp | 16 ++++++++++++++++ .../qml/qmllint/data/constructorProperty.qml | 5 +++++ tests/auto/qml/qmllint/tst_qmllint.cpp | 1 + 3 files changed, 22 insertions(+) create mode 100644 tests/auto/qml/qmllint/data/constructorProperty.qml diff --git a/src/qmlcompiler/qqmljstypepropagator.cpp b/src/qmlcompiler/qqmljstypepropagator.cpp index 4e8f598b32..153e93fdbc 100644 --- a/src/qmlcompiler/qqmljstypepropagator.cpp +++ b/src/qmlcompiler/qqmljstypepropagator.cpp @@ -487,6 +487,22 @@ bool QQmlJSTypePropagator::isRestricted(const QString &propertyName) const restrictedKind = u"an unscoped enum"_s; } } else if (accumulatorIn.value().content.isMethod()) { + auto overloadSet = accumulatorIn.value().content.method(); + auto potentiallyJSMethod = std::any_of( + overloadSet.cbegin(), overloadSet.cend(), + [](const QQmlJSMetaMethod &overload){ + return overload.isJavaScriptFunction(); + }); + if (potentiallyJSMethod) { + /* JS global constructors like Number get detected as methods + However, they still have properties that can be accessed + e.g. Number.EPSILON. This also isn't restricted to constructor + functions, so use isJavaScriptFunction as an overapproximation. + That catches also QQmlV4Function, but we're purging uses of it + anyway. + */ + return false; + } restrictedKind = u"a method"_s; } diff --git a/tests/auto/qml/qmllint/data/constructorProperty.qml b/tests/auto/qml/qmllint/data/constructorProperty.qml new file mode 100644 index 0000000000..d7ef884283 --- /dev/null +++ b/tests/auto/qml/qmllint/data/constructorProperty.qml @@ -0,0 +1,5 @@ +import QtQml + +QtObject { + property double f: Number.EPSILON +} diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp index 2bfd794162..ce5e24ed5e 100644 --- a/tests/auto/qml/qmllint/tst_qmllint.cpp +++ b/tests/auto/qml/qmllint/tst_qmllint.cpp @@ -1198,6 +1198,7 @@ void TestQmllint::cleanQmlCode_data() QTest::newRow("qtquickdialog") << QStringLiteral("qtquickdialog.qml"); QTest::newRow("callBase") << QStringLiteral("callBase.qml"); QTest::newRow("propertyWithOn") << QStringLiteral("switcher.qml"); + QTest::newRow("constructorProperty") << QStringLiteral("constructorProperty.qml"); } void TestQmllint::cleanQmlCode()