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 <fabian.kosmale@qt.io>
(cherry picked from commit 31d834a1c0d83d22fcf74624577013a558ad1974)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-06-22 09:38:08 +02:00 committed by Qt Cherry-pick Bot
parent 76a051047e
commit 38787f9507
9 changed files with 24 additions and 15 deletions

View File

@ -43,7 +43,7 @@ class QSharedMemoryLocker
{ {
public: public:
inline QSharedMemoryLocker(QSharedMemory *sharedMemory) : q_sm(sharedMemory) Q_NODISCARD_CTOR QSharedMemoryLocker(QSharedMemory *sharedMemory) : q_sm(sharedMemory)
{ {
Q_ASSERT(q_sm); Q_ASSERT(q_sm);
} }

View File

@ -54,8 +54,11 @@ class QEventLoopLockerPrivate;
class Q_CORE_EXPORT QEventLoopLocker class Q_CORE_EXPORT QEventLoopLocker
{ {
public: public:
Q_NODISCARD_CTOR
QEventLoopLocker(); QEventLoopLocker();
Q_NODISCARD_CTOR
explicit QEventLoopLocker(QEventLoop *loop); explicit QEventLoopLocker(QEventLoop *loop);
Q_NODISCARD_CTOR
explicit QEventLoopLocker(QThread *thread); explicit QEventLoopLocker(QThread *thread);
~QEventLoopLocker(); ~QEventLoopLocker();

View File

@ -19,16 +19,16 @@ namespace QtPrivate {
QTaggedPointer<Storage, Tag> m_pointer; QTaggedPointer<Storage, Tag> m_pointer;
public: 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<Storage *>(const_cast<void *>(pointer)), Const) : m_pointer(reinterpret_cast<Storage *>(const_cast<void *>(pointer)), Const)
{ {
Q_UNUSED(alignment); Q_UNUSED(alignment);
Q_ASSERT(alignment > qsizetype(alignof(Storage))); Q_ASSERT(alignment > qsizetype(alignof(Storage)));
} }
QConstPreservingPointer(void *pointer, qsizetype alignment) Q_NODISCARD_CTOR QConstPreservingPointer(void *pointer, qsizetype alignment)
: m_pointer(reinterpret_cast<Storage *>(pointer), Mutable) : m_pointer(reinterpret_cast<Storage *>(pointer), Mutable)
{ {
Q_UNUSED(alignment); Q_UNUSED(alignment);
@ -36,20 +36,20 @@ namespace QtPrivate {
} }
template<typename InputType> template<typename InputType>
QConstPreservingPointer(const InputType *pointer) Q_NODISCARD_CTOR QConstPreservingPointer(const InputType *pointer)
: m_pointer(reinterpret_cast<Storage *>(const_cast<InputType *>(pointer)), Const) : m_pointer(reinterpret_cast<Storage *>(const_cast<InputType *>(pointer)), Const)
{ {
static_assert(alignof(InputType) >= alignof(Storage)); static_assert(alignof(InputType) >= alignof(Storage));
} }
template<typename InputType> template<typename InputType>
QConstPreservingPointer(InputType *pointer) Q_NODISCARD_CTOR QConstPreservingPointer(InputType *pointer)
: m_pointer(reinterpret_cast<Storage *>(pointer), Mutable) : m_pointer(reinterpret_cast<Storage *>(pointer), Mutable)
{ {
static_assert(alignof(InputType) >= alignof(Storage)); static_assert(alignof(InputType) >= alignof(Storage));
} }
QConstPreservingPointer() = default; Q_NODISCARD_CTOR QConstPreservingPointer() = default;
const Type *constPointer() const const Type *constPointer() const
{ {

View File

@ -27,27 +27,32 @@ public:
typedef typename std::conditional<pass_parameter_by_value, T, const T &>::type parameter_type; typedef typename std::conditional<pass_parameter_by_value, T, const T &>::type parameter_type;
Q_NODISCARD_CTOR
constexpr QArrayDataPointer() noexcept constexpr QArrayDataPointer() noexcept
: d(nullptr), ptr(nullptr), size(0) : d(nullptr), ptr(nullptr), size(0)
{ {
} }
Q_NODISCARD_CTOR
QArrayDataPointer(const QArrayDataPointer &other) noexcept QArrayDataPointer(const QArrayDataPointer &other) noexcept
: d(other.d), ptr(other.ptr), size(other.size) : d(other.d), ptr(other.ptr), size(other.size)
{ {
ref(); ref();
} }
Q_NODISCARD_CTOR
constexpr QArrayDataPointer(Data *header, T *adata, qsizetype n = 0) noexcept constexpr QArrayDataPointer(Data *header, T *adata, qsizetype n = 0) noexcept
: d(header), ptr(adata), size(n) : d(header), ptr(adata), size(n)
{ {
} }
Q_NODISCARD_CTOR
explicit QArrayDataPointer(QPair<QTypedArrayData<T> *, T *> adata, qsizetype n = 0) noexcept explicit QArrayDataPointer(QPair<QTypedArrayData<T> *, T *> adata, qsizetype n = 0) noexcept
: d(adata.first), ptr(adata.second), size(n) : d(adata.first), ptr(adata.second), size(n)
{ {
} }
Q_NODISCARD_CTOR
static QArrayDataPointer fromRawData(const T *rawData, qsizetype length) noexcept static QArrayDataPointer fromRawData(const T *rawData, qsizetype length) noexcept
{ {
Q_ASSERT(rawData || !length); Q_ASSERT(rawData || !length);
@ -61,6 +66,7 @@ public:
return *this; return *this;
} }
Q_NODISCARD_CTOR
QArrayDataPointer(QArrayDataPointer &&other) noexcept QArrayDataPointer(QArrayDataPointer &&other) noexcept
: d(other.d), ptr(other.ptr), size(other.size) : d(other.d), ptr(other.ptr), size(other.size)
{ {

View File

@ -43,10 +43,10 @@ public:
static constexpr quintptr tagMask() { return QtPrivate::TagInfo<T>::alignment - 1; } static constexpr quintptr tagMask() { return QtPrivate::TagInfo<T>::alignment - 1; }
static constexpr quintptr pointerMask() { return ~tagMask(); } static constexpr quintptr pointerMask() { return ~tagMask(); }
constexpr QTaggedPointer() noexcept : d(0) {} Q_NODISCARD_CTOR constexpr QTaggedPointer() noexcept : d(0) {}
constexpr QTaggedPointer(std::nullptr_t) noexcept : QTaggedPointer() {} 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)) : d(quintptr(pointer) | quintptr(tag))
{ {
static_assert(sizeof(Type*) == sizeof(QTaggedPointer)); static_assert(sizeof(Type*) == sizeof(QTaggedPointer));

View File

@ -1944,7 +1944,7 @@ bool QDBusConnectionPrivate::send(const QDBusMessage& message)
class QDBusBlockingCallWatcher class QDBusBlockingCallWatcher
{ {
public: public:
QDBusBlockingCallWatcher(const QDBusMessage &message) Q_NODISCARD_CTOR QDBusBlockingCallWatcher(const QDBusMessage &message)
: m_message(message), m_maxCallTimeoutMs(0) : m_message(message), m_maxCallTimeoutMs(0)
{ {
#if defined(QT_NO_DEBUG) #if defined(QT_NO_DEBUG)

View File

@ -93,7 +93,7 @@ struct QDBusReadLocker: QDBusLockerBase
{ {
QDBusConnectionPrivate *self; QDBusConnectionPrivate *self;
ThreadAction action; ThreadAction action;
inline QDBusReadLocker(ThreadAction a, QDBusConnectionPrivate *s) Q_NODISCARD_CTOR QDBusReadLocker(ThreadAction a, QDBusConnectionPrivate *s)
: self(s), action(a) : self(s), action(a)
{ {
reportThreadAction(action, BeforeLock, self); reportThreadAction(action, BeforeLock, self);
@ -113,7 +113,7 @@ struct QDBusWriteLocker: QDBusLockerBase
{ {
QDBusConnectionPrivate *self; QDBusConnectionPrivate *self;
ThreadAction action; ThreadAction action;
inline QDBusWriteLocker(ThreadAction a, QDBusConnectionPrivate *s) Q_NODISCARD_CTOR QDBusWriteLocker(ThreadAction a, QDBusConnectionPrivate *s)
: self(s), action(a) : self(s), action(a)
{ {
reportThreadAction(action, BeforeLock, self); reportThreadAction(action, BeforeLock, self);

View File

@ -62,7 +62,7 @@ template <typename T>
class QDirectFBPointer : public QScopedPointer<T, QDirectFBInterfaceCleanupHandler<T> > class QDirectFBPointer : public QScopedPointer<T, QDirectFBInterfaceCleanupHandler<T> >
{ {
public: public:
QDirectFBPointer(T *t = nullptr) Q_NODISCARD_CTOR QDirectFBPointer(T *t = nullptr)
: QScopedPointer<T, QDirectFBInterfaceCleanupHandler<T> >(t) : QScopedPointer<T, QDirectFBInterfaceCleanupHandler<T> >(t)
{} {}

View File

@ -367,7 +367,7 @@ Q_DECLARE_TYPEINFO(QXcbConnection::TabletData, Q_RELOCATABLE_TYPE);
class QXcbConnectionGrabber class QXcbConnectionGrabber
{ {
public: public:
QXcbConnectionGrabber(QXcbConnection *connection); Q_NODISCARD_CTOR QXcbConnectionGrabber(QXcbConnection *connection);
~QXcbConnectionGrabber(); ~QXcbConnectionGrabber();
void release(); void release();
private: private: