From 38787f9507630fe096fd0039b8cef9df7b5c4e54 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 22 Jun 2023 09:38:08 +0200 Subject: [PATCH] Mark all remaining RAII/smart pointer class ctors [[nodiscard]] ... like QUIP-0019 suggests. The main problem here is finding these classes. We don't have markup for RAII classes, so I had to find them by name. This patch is based on the output of git grep -we Q[A-Z0-9a-z_]+er extracting the matches and piping them through sort -u, then removing a lot of suffixes like Manager and Handler, then visually inspecting the remaining list. Task-number: QTBUG-104164 Change-Id: I59b18d8d0a0237fcc11047857adc39b984ad7fcb Reviewed-by: Fabian Kosmale (cherry picked from commit 31d834a1c0d83d22fcf74624577013a558ad1974) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/ipc/qsharedmemory_p.h | 2 +- src/corelib/kernel/qeventloop.h | 3 +++ src/corelib/kernel/qiterable.h | 12 ++++++------ src/corelib/tools/qarraydatapointer.h | 6 ++++++ src/corelib/tools/qtaggedpointer.h | 6 +++--- src/dbus/qdbusintegrator.cpp | 2 +- src/dbus/qdbusthreaddebug_p.h | 4 ++-- .../platforms/directfb/qdirectfbconvenience.h | 2 +- src/plugins/platforms/xcb/qxcbconnection.h | 2 +- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/corelib/ipc/qsharedmemory_p.h b/src/corelib/ipc/qsharedmemory_p.h index 3a6cd8c137f..94194babf37 100644 --- a/src/corelib/ipc/qsharedmemory_p.h +++ b/src/corelib/ipc/qsharedmemory_p.h @@ -43,7 +43,7 @@ class QSharedMemoryLocker { public: - inline QSharedMemoryLocker(QSharedMemory *sharedMemory) : q_sm(sharedMemory) + Q_NODISCARD_CTOR QSharedMemoryLocker(QSharedMemory *sharedMemory) : q_sm(sharedMemory) { Q_ASSERT(q_sm); } diff --git a/src/corelib/kernel/qeventloop.h b/src/corelib/kernel/qeventloop.h index 1da1d2336e3..387fcbbeaa4 100644 --- a/src/corelib/kernel/qeventloop.h +++ b/src/corelib/kernel/qeventloop.h @@ -54,8 +54,11 @@ class QEventLoopLockerPrivate; class Q_CORE_EXPORT QEventLoopLocker { public: + Q_NODISCARD_CTOR QEventLoopLocker(); + Q_NODISCARD_CTOR explicit QEventLoopLocker(QEventLoop *loop); + Q_NODISCARD_CTOR explicit QEventLoopLocker(QThread *thread); ~QEventLoopLocker(); diff --git a/src/corelib/kernel/qiterable.h b/src/corelib/kernel/qiterable.h index 11bf82dc043..1178c5e8a34 100644 --- a/src/corelib/kernel/qiterable.h +++ b/src/corelib/kernel/qiterable.h @@ -19,16 +19,16 @@ namespace QtPrivate { QTaggedPointer m_pointer; public: - QConstPreservingPointer(std::nullptr_t) : m_pointer(nullptr, Const) {} + Q_NODISCARD_CTOR QConstPreservingPointer(std::nullptr_t) : m_pointer(nullptr, Const) {} - QConstPreservingPointer(const void *pointer, qsizetype alignment) + Q_NODISCARD_CTOR QConstPreservingPointer(const void *pointer, qsizetype alignment) : m_pointer(reinterpret_cast(const_cast(pointer)), Const) { Q_UNUSED(alignment); Q_ASSERT(alignment > qsizetype(alignof(Storage))); } - QConstPreservingPointer(void *pointer, qsizetype alignment) + Q_NODISCARD_CTOR QConstPreservingPointer(void *pointer, qsizetype alignment) : m_pointer(reinterpret_cast(pointer), Mutable) { Q_UNUSED(alignment); @@ -36,20 +36,20 @@ namespace QtPrivate { } template - QConstPreservingPointer(const InputType *pointer) + Q_NODISCARD_CTOR QConstPreservingPointer(const InputType *pointer) : m_pointer(reinterpret_cast(const_cast(pointer)), Const) { static_assert(alignof(InputType) >= alignof(Storage)); } template - QConstPreservingPointer(InputType *pointer) + Q_NODISCARD_CTOR QConstPreservingPointer(InputType *pointer) : m_pointer(reinterpret_cast(pointer), Mutable) { static_assert(alignof(InputType) >= alignof(Storage)); } - QConstPreservingPointer() = default; + Q_NODISCARD_CTOR QConstPreservingPointer() = default; const Type *constPointer() const { diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index eb20330f0f4..258e7b7446b 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -27,27 +27,32 @@ public: typedef typename std::conditional::type parameter_type; + Q_NODISCARD_CTOR constexpr QArrayDataPointer() noexcept : d(nullptr), ptr(nullptr), size(0) { } + Q_NODISCARD_CTOR QArrayDataPointer(const QArrayDataPointer &other) noexcept : d(other.d), ptr(other.ptr), size(other.size) { ref(); } + Q_NODISCARD_CTOR constexpr QArrayDataPointer(Data *header, T *adata, qsizetype n = 0) noexcept : d(header), ptr(adata), size(n) { } + Q_NODISCARD_CTOR explicit QArrayDataPointer(QPair *, T *> adata, qsizetype n = 0) noexcept : d(adata.first), ptr(adata.second), size(n) { } + Q_NODISCARD_CTOR static QArrayDataPointer fromRawData(const T *rawData, qsizetype length) noexcept { Q_ASSERT(rawData || !length); @@ -61,6 +66,7 @@ public: return *this; } + Q_NODISCARD_CTOR QArrayDataPointer(QArrayDataPointer &&other) noexcept : d(other.d), ptr(other.ptr), size(other.size) { diff --git a/src/corelib/tools/qtaggedpointer.h b/src/corelib/tools/qtaggedpointer.h index bc43f765aa9..6c467d59f84 100644 --- a/src/corelib/tools/qtaggedpointer.h +++ b/src/corelib/tools/qtaggedpointer.h @@ -43,10 +43,10 @@ public: static constexpr quintptr tagMask() { return QtPrivate::TagInfo::alignment - 1; } static constexpr quintptr pointerMask() { return ~tagMask(); } - constexpr QTaggedPointer() noexcept : d(0) {} - constexpr QTaggedPointer(std::nullptr_t) noexcept : QTaggedPointer() {} + Q_NODISCARD_CTOR constexpr QTaggedPointer() noexcept : d(0) {} + Q_NODISCARD_CTOR constexpr QTaggedPointer(std::nullptr_t) noexcept : QTaggedPointer() {} - explicit QTaggedPointer(T *pointer, Tag tag = Tag()) noexcept + Q_NODISCARD_CTOR explicit QTaggedPointer(T *pointer, Tag tag = Tag()) noexcept : d(quintptr(pointer) | quintptr(tag)) { static_assert(sizeof(Type*) == sizeof(QTaggedPointer)); diff --git a/src/dbus/qdbusintegrator.cpp b/src/dbus/qdbusintegrator.cpp index 4d67875c75f..c978252efeb 100644 --- a/src/dbus/qdbusintegrator.cpp +++ b/src/dbus/qdbusintegrator.cpp @@ -1944,7 +1944,7 @@ bool QDBusConnectionPrivate::send(const QDBusMessage& message) class QDBusBlockingCallWatcher { public: - QDBusBlockingCallWatcher(const QDBusMessage &message) + Q_NODISCARD_CTOR QDBusBlockingCallWatcher(const QDBusMessage &message) : m_message(message), m_maxCallTimeoutMs(0) { #if defined(QT_NO_DEBUG) diff --git a/src/dbus/qdbusthreaddebug_p.h b/src/dbus/qdbusthreaddebug_p.h index 73be3d75177..a1d3a420ec6 100644 --- a/src/dbus/qdbusthreaddebug_p.h +++ b/src/dbus/qdbusthreaddebug_p.h @@ -93,7 +93,7 @@ struct QDBusReadLocker: QDBusLockerBase { QDBusConnectionPrivate *self; ThreadAction action; - inline QDBusReadLocker(ThreadAction a, QDBusConnectionPrivate *s) + Q_NODISCARD_CTOR QDBusReadLocker(ThreadAction a, QDBusConnectionPrivate *s) : self(s), action(a) { reportThreadAction(action, BeforeLock, self); @@ -113,7 +113,7 @@ struct QDBusWriteLocker: QDBusLockerBase { QDBusConnectionPrivate *self; ThreadAction action; - inline QDBusWriteLocker(ThreadAction a, QDBusConnectionPrivate *s) + Q_NODISCARD_CTOR QDBusWriteLocker(ThreadAction a, QDBusConnectionPrivate *s) : self(s), action(a) { reportThreadAction(action, BeforeLock, self); diff --git a/src/plugins/platforms/directfb/qdirectfbconvenience.h b/src/plugins/platforms/directfb/qdirectfbconvenience.h index c013988fe28..dc657f384e6 100644 --- a/src/plugins/platforms/directfb/qdirectfbconvenience.h +++ b/src/plugins/platforms/directfb/qdirectfbconvenience.h @@ -62,7 +62,7 @@ template class QDirectFBPointer : public QScopedPointer > { public: - QDirectFBPointer(T *t = nullptr) + Q_NODISCARD_CTOR QDirectFBPointer(T *t = nullptr) : QScopedPointer >(t) {} diff --git a/src/plugins/platforms/xcb/qxcbconnection.h b/src/plugins/platforms/xcb/qxcbconnection.h index 24e684866d8..abc17b210ee 100644 --- a/src/plugins/platforms/xcb/qxcbconnection.h +++ b/src/plugins/platforms/xcb/qxcbconnection.h @@ -367,7 +367,7 @@ Q_DECLARE_TYPEINFO(QXcbConnection::TabletData, Q_RELOCATABLE_TYPE); class QXcbConnectionGrabber { public: - QXcbConnectionGrabber(QXcbConnection *connection); + Q_NODISCARD_CTOR QXcbConnectionGrabber(QXcbConnection *connection); ~QXcbConnectionGrabber(); void release(); private: