aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qqmljsimportvisitor.cpp
diff options
context:
space:
mode:
authorOlivier De Cannière <olivier.decanniere@qt.io>2025-06-05 16:50:26 +0200
committerOlivier De Cannière <olivier.decanniere@qt.io>2025-06-10 13:25:50 +0200
commit2a6e8582fc2477e8ff670b38bf8d6dc32aed14ca (patch)
treea0eb2fee96986840b74ed82440bad4dd83aee43e /src/qmlcompiler/qqmljsimportvisitor.cpp
parentf64ba5dec875927163bb917597bac51540eb4097 (diff)
qmllint: Warn about using enums as type annotations in functions
Until we support a proper representation of enums in the type system, all we can do is warn the user that enums can't be used as types. After the improved runtime warning added in a recent commit, this patch adds a qmllint warning to warn about the same issue but at 'compile time'. Fixes: QTBUG-135255 Pick-to: 6.10 6.9 6.8 6.5 Change-Id: I02266f21266e59eb6d4a4dbe0e4335b845c62133 Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'src/qmlcompiler/qqmljsimportvisitor.cpp')
-rw-r--r--src/qmlcompiler/qqmljsimportvisitor.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmljsimportvisitor.cpp b/src/qmlcompiler/qqmljsimportvisitor.cpp
index b55dbb4d60..92b5319347 100644
--- a/src/qmlcompiler/qqmljsimportvisitor.cpp
+++ b/src/qmlcompiler/qqmljsimportvisitor.cpp
@@ -759,10 +759,40 @@ void QQmlJSImportVisitor::processPropertyTypes()
void QQmlJSImportVisitor::processMethodTypes()
{
+ const auto isEnumUsedAsType = [&](QStringView typeName, const QQmlJS::SourceLocation &loc) {
+ if (typeName == "enum"_L1) {
+ m_logger->log("QML does not have an `enum` type. Use the enum's underlying type "
+ "(int or double)."_L1,
+ qmlEnumsAreNotTypes, loc);
+ return true;
+ }
+
+ const auto split = typeName.tokenize(u'.').toContainer<QVarLengthArray<QStringView, 4>>();
+ if (split.size() != 2)
+ return false;
+
+ const QStringView scopeName = split[0];
+ const QStringView enumName = split[1];
+
+ if (auto scope = QQmlJSScope::findType(scopeName.toString(),
+ m_rootScopeImports.contextualTypes()).scope) {
+ if (scope->enumeration(enumName.toString()).isValid()) {
+ m_logger->log("QML enumerations are not types. Use underlying type "
+ "(int or double) instead."_L1,
+ qmlEnumsAreNotTypes, loc);
+ return true;
+ }
+ }
+ return false;
+ };
+
for (const auto &method : std::as_const(m_pendingMethodTypeAnnotations)) {
for (auto [it, end] = method.scope->mutableOwnMethodsRange(method.methodName); it != end; ++it) {
const auto [parameterBegin, parameterEnd] = it->mutableParametersRange();
for (auto parameter = parameterBegin; parameter != parameterEnd; ++parameter) {
+ const int parameterIndex = parameter - parameterBegin;
+ if (isEnumUsedAsType(parameter->typeName(), method.locations[parameterIndex]))
+ continue;
if (const auto parameterType = QQmlJSScope::findType(
parameter->typeName(), m_rootScopeImports.contextualTypes()).scope) {
parameter->setType({ parameterType });
@@ -774,6 +804,8 @@ void QQmlJSImportVisitor::processMethodTypes()
}
}
+ if (isEnumUsedAsType(it->returnTypeName(), method.locations.last()))
+ continue;
if (const auto returnType = QQmlJSScope::findType(
it->returnTypeName(), m_rootScopeImports.contextualTypes()).scope) {
it->setReturnType({ returnType });