aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-09-18 10:53:19 +0200
committerUlf Hermann <ulf.hermann@qt.io>2023-09-19 14:34:43 +0200
commitbe33fee1addf84971a63f645a2f48ab84a25bae2 (patch)
tree7ea905fa2b29686e4abc449f4f1f9266bb2c4916 /src
parent5e3b3b25f2ff7968f7b51733128345f2762f35c9 (diff)
QmlDom: Do not needlessly copy things into lambdas
Where we have to, make it explicit and add comments. Coverity-Id: 417204 Coverity-Id: 417197 Coverity-Id: 417196 Coverity-Id: 417188 Coverity-Id: 417186 Change-Id: I33e01d4f9a9cbf213119446808e902c5d83b71ac Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qmldom/qqmldomelements.cpp10
-rw-r--r--src/qmldom/qqmldomitem.cpp6
-rw-r--r--src/qmldom/qqmldomtop.cpp12
3 files changed, 16 insertions, 12 deletions
diff --git a/src/qmldom/qqmldomelements.cpp b/src/qmldom/qqmldomelements.cpp
index 73d9efa3de..6704737ea5 100644
--- a/src/qmldom/qqmldomelements.cpp
+++ b/src/qmldom/qqmldomelements.cpp
@@ -462,13 +462,13 @@ DomItem QmlObject::field(const DomItem &self, QStringView name) const
break;
case 13:
if (name == Fields::propertyInfos)
+ // Need to explicitly copy self here since we might store this and call it later.
return self.subMapItem(Map(
pathFromOwner().field(Fields::propertyInfos),
- [self](const DomItem &map, QString k) mutable {
- auto pInfo = self.propertyInfoWithName(k);
- return map.wrap(PathEls::Key(k), pInfo);
+ [copiedSelf = self](const DomItem &map, const QString &k) {
+ return map.wrap(PathEls::Key(k), copiedSelf.propertyInfoWithName(k));
},
- [self](const DomItem &) mutable { return self.propertyInfoNames(); },
+ [copiedSelf = self](const DomItem &) { return copiedSelf.propertyInfoNames(); },
QLatin1String("PropertyInfo")));
break;
case 19:
@@ -1481,7 +1481,7 @@ DomItem BindingValue::value(const DomItem &binding) const
case BindingValueKind::Array:
return binding.subListItem(List::fromQListRef<QmlObject>(
binding.pathFromOwner().field(u"value"), array,
- [binding](const DomItem &self, const PathEls::PathComponent &, const QmlObject &obj) {
+ [](const DomItem &self, const PathEls::PathComponent &, const QmlObject &obj) {
return self.copy(&obj);
}));
}
diff --git a/src/qmldom/qqmldomitem.cpp b/src/qmldom/qqmldomitem.cpp
index a5478012d9..57b8adfd47 100644
--- a/src/qmldom/qqmldomitem.cpp
+++ b/src/qmldom/qqmldomitem.cpp
@@ -1112,7 +1112,7 @@ DomItem DomItem::path(Path p, ErrorHandler errorHandler) const
if (!p)
return *this;
DomItem res;
- resolve(p, [&res](Path, DomItem it) {
+ resolve(p, [&res](Path, const DomItem &it) {
res = it;
return false;
}, errorHandler);
@@ -2481,8 +2481,8 @@ void DomItem::addError(ErrorMessage &&msg) const
ErrorHandler DomItem::errorHandler() const
{
- DomItem self = *this;
- return [self](const ErrorMessage &m) { self.addError(ErrorMessage(m)); };
+ // We need a copy here. Error handlers may be called when this is gone.
+ return [self = *this](const ErrorMessage &m) { self.addError(ErrorMessage(m)); };
}
void DomItem::clearErrors(ErrorGroups groups, bool iterate) const
diff --git a/src/qmldom/qqmldomtop.cpp b/src/qmldom/qqmldomtop.cpp
index 95f7b069d4..fefdd370c2 100644
--- a/src/qmldom/qqmldomtop.cpp
+++ b/src/qmldom/qqmldomtop.cpp
@@ -616,8 +616,10 @@ void LoadInfo::advanceLoad(const DomItem &self)
if (!dep.uri.isEmpty()) {
self.loadModuleDependency(
dep.uri, dep.version,
- [this, self, dep](Path, const DomItem &, const DomItem &) mutable {
- finishedLoadingDep(self, dep);
+ [this, copiedSelf = self, dep](Path, const DomItem &, const DomItem &) {
+ // Need to explicitly copy self here since we might store this and
+ // call it later.
+ finishedLoadingDep(copiedSelf, dep);
},
self.errorHandler());
Q_ASSERT(dep.filePath.isEmpty() && "dependency with both uri and file");
@@ -626,8 +628,10 @@ void LoadInfo::advanceLoad(const DomItem &self)
if (std::shared_ptr<DomEnvironment> envPtr = env.ownerAs<DomEnvironment>())
envPtr->loadFile(
env, FileToLoad::fromFileSystem(envPtr, dep.filePath),
- [this, self, dep](Path, const DomItem &, const DomItem &) mutable {
- finishedLoadingDep(self, dep);
+ [this, copiedSelf = self, dep](Path, const DomItem &, const DomItem &) {
+ // Need to explicitly copy self here since we might store this and
+ // call it later.
+ finishedLoadingDep(copiedSelf, dep);
},
nullptr, nullptr, LoadOption::DefaultLoad, dep.fileType,
self.errorHandler());