Restrict M16 warnings

Do not warn out invalid property name if the property starts with an id
in the same component scope.

Small refactoring: Move early-return-likely elements up before any
calculations done.

Fixes: QTCREATORBUG-28468
Change-Id: I2cbdbc24af42f126db0bbd6027ebe4f96d9c199f
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Semih Yavuz
2023-06-09 14:27:00 +02:00
parent 3664a62f98
commit 012f984c27
2 changed files with 58 additions and 17 deletions

View File

@@ -2003,10 +2003,20 @@ bool Check::visit(TypeOfExpression *ast)
/// ### Maybe put this into the context as a helper function. /// ### Maybe put this into the context as a helper function.
const Value *Check::checkScopeObjectMember(const UiQualifiedId *id) const Value *Check::checkScopeObjectMember(const UiQualifiedId *id)
{ {
if (!_importsOk) if (!_importsOk)
return nullptr; return nullptr;
if (!id)
return nullptr; // ### error?
if (id->name.isEmpty()) // possible after error recovery
return nullptr;
QString propertyName = id->name.toString();
if (propertyName == "id" && !id->next)
return nullptr; // ### should probably be a special value
QList<const ObjectValue *> scopeObjects = _scopeChain.qmlScopeObjects(); QList<const ObjectValue *> scopeObjects = _scopeChain.qmlScopeObjects();
if (scopeObjects.isEmpty()) if (scopeObjects.isEmpty())
return nullptr; return nullptr;
@@ -2021,24 +2031,9 @@ const Value *Check::checkScopeObjectMember(const UiQualifiedId *id)
return isAttachedProperty; return isAttachedProperty;
}; };
if (! id)
return nullptr; // ### error?
if (id->name.isEmpty()) // possible after error recovery
return nullptr;
QString propertyName = id->name.toString();
if (propertyName == "id" && !id->next)
return nullptr; // ### should probably be a special value
// attached properties // attached properties
bool isAttachedProperty = getAttachedTypes(propertyName); bool isAttachedProperty = getAttachedTypes(propertyName);
if (scopeObjects.isEmpty())
return nullptr;
// global lookup for first part of id // global lookup for first part of id
const Value *value = nullptr; const Value *value = nullptr;
for (int i = scopeObjects.size() - 1; i >= 0; --i) { for (int i = scopeObjects.size() - 1; i >= 0; --i) {
@@ -2053,6 +2048,13 @@ const Value *Check::checkScopeObjectMember(const UiQualifiedId *id)
return nullptr; return nullptr;
if (!value) { if (!value) {
// We omit M16 messages if the type using ImmediateProperties
// Ideally, we should obtain them through metaobject information
const bool omitMessage = !m_typeStack.isEmpty()
&& ((m_typeStack.last() == "PropertyChanges")
|| m_typeStack.last() == "Binding")
&& !m_idStack.isEmpty() && m_idStack.last().contains(propertyName);
if (!omitMessage)
addMessage(ErrInvalidPropertyName, id->identifierToken, propertyName); addMessage(ErrInvalidPropertyName, id->identifierToken, propertyName);
return nullptr; return nullptr;
} }

View File

@@ -0,0 +1,39 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
import QtQuick
import QtQuick.Window
Window {
id: root
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
id: rect
Rectangle {
id: innerRect
}
}
Text {
id: myText
width: 50
wrapMode: Text.WordWrap
text: "a text string that is longer than 50 pixels"
Text {
id: innerText
}
states: State {
name: "widerText"
PropertyChanges { myText.width: undefined }
AnchorChanges { innerRect.width: undefined } // 16 29 37
}
}
Binding {rect.width: innerText.width}
}