diff options
author | Ulf Hermann <[email protected]> | 2024-12-17 17:15:56 +0100 |
---|---|---|
committer | Ulf Hermann <[email protected]> | 2024-12-19 12:39:37 +0100 |
commit | 42930796f4d4ba8d3be49bb63572f658402263e0 (patch) | |
tree | eefcc0ff7a6f260c3aa7602406328f5168e71c85 /src/qmlcompiler/qqmljsliteralbindingcheck.cpp | |
parent | 6c3149e51e86349c41bbf4ad5141bbfe3825e1ea (diff) |
qmllint: Warn about assignment to read-only properties only once
The basic literal binding check should do this. The QtQuick-specific one
should only warn about QtQuick-specific issues.
Pick-to: 6.9 6.8
Fixes: QTBUG-127691
Change-Id: I0d7c6113456091657362cbd123f730e5101e6cee
Reviewed-by: Semih Yavuz <[email protected]>
Diffstat (limited to 'src/qmlcompiler/qqmljsliteralbindingcheck.cpp')
-rw-r--r-- | src/qmlcompiler/qqmljsliteralbindingcheck.cpp | 59 |
1 files changed, 26 insertions, 33 deletions
diff --git a/src/qmlcompiler/qqmljsliteralbindingcheck.cpp b/src/qmlcompiler/qqmljsliteralbindingcheck.cpp index c2498afbed..f136bf38da 100644 --- a/src/qmlcompiler/qqmljsliteralbindingcheck.cpp +++ b/src/qmlcompiler/qqmljsliteralbindingcheck.cpp @@ -93,44 +93,26 @@ QQmlSA::Property LiteralBindingCheckBase::getProperty(const QString &propertyNam return bindingScope.property(unqualifiedPropertyName); } - -void LiteralBindingCheckBase::onBinding(const QQmlSA::Element &element, const QString &propertyName, - const QQmlSA::Binding &binding, - const QQmlSA::Element &bindingScope, - const QQmlSA::Element &value) +void LiteralBindingCheckBase::warnOnCheckedBinding( + const QQmlSA::Binding &binding, const QQmlSA::Element &propertyType) { - Q_UNUSED(value); - - const auto property = getProperty(propertyName, binding, bindingScope); - if (!property.isValid()) + auto construction = check(propertyType.internalId(), binding.stringValue()); + if (!construction.isValid()) return; - // If the property is defined in the same scope where it is set, - // we are in fact allowed to set it, even if it's not writable. - if (property.isReadonly() && !element.hasOwnProperty(propertyName)) { - emitWarning(u"Cannot assign to read-only property %1"_s.arg(propertyName), - qmlReadOnlyProperty, binding.sourceLocation()); + const QString warningMessage = + u"Construction from string is deprecated. Use structured value type " + u"construction instead for type \"%1\""_s.arg(propertyType.internalId()); + + if (construction.code.isEmpty()) { + emitWarning(warningMessage, qmlIncompatibleType, binding.sourceLocation()); return; } - if (auto propertyType = property.type(); propertyType) { - auto construction = check(propertyType.internalId(), binding.stringValue()); - if (construction.isValid()) { - const QString warningMessage = - u"Construction from string is deprecated. Use structured value type " - u"construction instead for type \"%1\""_s.arg(propertyType.internalId()); - - if (!construction.code.isNull()) { - QQmlSA::FixSuggestion suggestion( - u"Replace string by structured value construction"_s, - binding.sourceLocation(), construction.code); - emitWarning(warningMessage, qmlIncompatibleType, binding.sourceLocation(), suggestion); - return; - } - emitWarning(warningMessage, qmlIncompatibleType, binding.sourceLocation()); - return; - } - } + const QQmlSA::FixSuggestion suggestion( + u"Replace string by structured value construction"_s, + binding.sourceLocation(), construction.code); + emitWarning(warningMessage, qmlIncompatibleType, binding.sourceLocation(), suggestion); } void QQmlJSLiteralBindingCheck::onBinding(const QQmlSA::Element &element, @@ -139,12 +121,23 @@ void QQmlJSLiteralBindingCheck::onBinding(const QQmlSA::Element &element, const QQmlSA::Element &bindingScope, const QQmlSA::Element &value) { - LiteralBindingCheckBase::onBinding(element, propertyName, binding, bindingScope, value); + Q_UNUSED(value); const auto property = getProperty(propertyName, binding, bindingScope); if (!property.isValid()) return; + // If the property is defined in the same scope where it is set, + // we are in fact allowed to set it, even if it's not writable. + if (property.isReadonly() && !element.hasOwnProperty(propertyName)) { + emitWarning(u"Cannot assign to read-only property %1"_s.arg(propertyName), + qmlReadOnlyProperty, binding.sourceLocation()); + return; + } + + if (const auto propertyType = property.type()) + warnOnCheckedBinding(binding, propertyType); + if (!canConvertForLiteralBinding(m_resolver, resolveLiteralType(binding), property.type())) { emitWarning(u"Cannot assign literal of type %1 to %2"_s.arg( literalPrettyTypeName(binding.bindingType()), |