qmllint: Warn about assigning values to read-only properties

Fixes: QTBUG-92844
Change-Id: Iff893296784043d86ba00fd9f08d28d85873d9b2
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Maximilian Goldstein 2021-08-20 14:02:54 +02:00
parent a1ed3c4dd7
commit 0984eb6ab0
4 changed files with 34 additions and 0 deletions

View File

@ -487,6 +487,15 @@ void QQmlJSTypePropagator::generate_StoreNameSloppy(int nameIndex)
return;
}
if (!type.isWritable()) {
setError(u"Can't assign to read-only property %1"_qs.arg(name));
m_logger->logWarning(u"Cannot assign to read-only property %1"_qs.arg(name), Log_Property,
getCurrentSourceLocation());
return;
}
if (!canConvertFromTo(m_state.accumulatorIn, type)) {
setError(u"cannot convert from %1 to %2"_qs
.arg(m_state.accumulatorIn.descriptiveName(), type.descriptiveName()));
@ -640,6 +649,10 @@ void QQmlJSTypePropagator::generate_StoreProperty(int nameIndex, int base)
if (!property.isWritable()) {
setError(u"Can't assign to read-only property %1"_qs.arg(propertyName));
m_logger->logWarning(u"Cannot assign to read-only property %1"_qs.arg(propertyName),
Log_Property, getCurrentSourceLocation());
return;
}

View File

@ -0,0 +1,6 @@
import QtQuick 2.0
Item {
activeFocus: true
Component.onCompleted: activeFocus = false
}

View File

@ -0,0 +1,7 @@
import QtQuick 2.0
Item {
id: item
activeFocus: true
Component.onCompleted: item.activeFocus = false
}

View File

@ -656,6 +656,14 @@ void TestQmllint::dirtyQmlCode_data()
<< QStringLiteral("Cannot use non-reference type QRectF as base "
"of namespaced attached type")
<< QString() << false;
QTest::newRow("AssignToReadOnlyProperty")
<< QStringLiteral("assignToReadOnlyProperty.qml")
<< QStringLiteral("Cannot assign to read-only property activeFocus") << QString()
<< false;
QTest::newRow("AssignToReadOnlyProperty")
<< QStringLiteral("assignToReadOnlyProperty2.qml")
<< QStringLiteral("Cannot assign to read-only property activeFocus") << QString()
<< false;
}
void TestQmllint::dirtyQmlCode()