Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmljsliteralbindingcheck.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <private/qqmljsimportvisitor_p.h>
7#include <private/qqmljstyperesolver_p.h>
8#include <private/qqmljsmetatypes_p.h>
9
11
12using namespace Qt::StringLiterals;
13
14// This makes no sense, but we want to warn about things QQmlPropertyResolver complains about.
16 QQmlJSTypeResolver *resolver, const QQmlJSScope::ConstPtr &from,
17 const QQmlJSScope::ConstPtr &to) {
18 if (resolver->equals(from, to))
19 return true;
20
21 if (!resolver->canConvertFromTo(from, to))
22 return false;
23
24 const bool fromIsString = resolver->equals(from, resolver->stringType());
25
26 if (resolver->equals(to, resolver->stringType())
27 || resolver->equals(to, resolver->stringListType())
28 || resolver->equals(to, resolver->byteArrayType())
29 || resolver->equals(to, resolver->urlType())) {
30 return fromIsString;
31 }
32
33 if (resolver->isNumeric(to))
34 return resolver->isNumeric(from);
35
36 if (resolver->equals(to, resolver->boolType()))
37 return resolver->equals(from, resolver->boolType());
38
39 return true;
40}
41
43{
44 QQmlJSLogger *logger = visitor->logger();
45 const auto literalScopes = visitor->literalScopesToCheck();
46 for (const auto &scope : literalScopes) {
47 const auto bindings = scope->ownPropertyBindings();
48 for (const auto &binding : bindings) {
49 if (!binding.hasLiteral())
50 continue;
51
52 const QString propertyName = binding.propertyName();
53 const QQmlJSMetaProperty property = scope->property(propertyName);
54 if (!property.isValid())
55 continue;
56
57 // If the property is defined in the same scope where it is set,
58 // we are in fact allowed to set it, even if it's not writable.
59 if (!property.isWritable() && !scope->hasOwnProperty(propertyName)) {
60 logger->log(u"Cannot assign to read-only property %1"_s.arg(propertyName),
61 qmlReadOnlyProperty, binding.sourceLocation());
62 continue;
63 }
64
66 resolver, binding.literalType(resolver), property.type())) {
67 logger->log(u"Cannot assign literal of type %1 to %2"_s.arg(
68 QQmlJSScope::prettyName(binding.literalTypeName()),
70 qmlIncompatibleType, binding.sourceLocation());
71 continue;
72 }
73 }
74 }
75}
76
const QQmlJSLogger * logger() const
QSet< QQmlJSScope::ConstPtr > literalScopesToCheck() const
void run(QQmlJSImportVisitor *visitor, QQmlJSTypeResolver *resolver)
void log(const QString &message, QQmlJS::LoggerWarningId id, const QQmlJS::SourceLocation &srcLocation, bool showContext=true, bool showFileName=true, const std::optional< QQmlJSFixSuggestion > &suggestion={}, const QString overrideFileName=QString())
static QString prettyName(QAnyStringView name)
bool equals(const QQmlJSScope::ConstPtr &a, const QQmlJSScope::ConstPtr &b) const
QQmlJSScope::ConstPtr stringType() const
bool isNumeric(const QQmlJSRegisterContent &type) const
QQmlJSScope::ConstPtr byteArrayType() const
QQmlJSScope::ConstPtr boolType() const
bool canConvertFromTo(const QQmlJSScope::ConstPtr &from, const QQmlJSScope::ConstPtr &to) const
QQmlJSScope::ConstPtr stringListType() const
QQmlJSScope::ConstPtr urlType() const
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
Combined button and popup list for selecting options.
static bool canConvertForLiteralBinding(QQmlJSTypeResolver *resolver, const QQmlJSScope::ConstPtr &from, const QQmlJSScope::ConstPtr &to)
const QQmlJS::LoggerWarningId qmlReadOnlyProperty
const QQmlJS::LoggerWarningId qmlIncompatibleType
const char property[13]
Definition qwizard.cpp:101