Plaster [[nodiscard]] on some RAII classes

The idea is to prevent silly mistakes such as

  QMutexLocker(mutex);
  doSomething();

where the locker is constructed and destroyed immediately. Compilers
don't normally warn in these cases (as the constructor/destructor
pairs involved do have side effects), but we can mark the type as
[[nodiscard]] to encourage warnings.

There is another couple of classes for which this would make sense
(notably, the R/W lockers), but unfortunately those are exported
classes, and GCC has a bug where one can't mix two different attribute
syntaxes on the same entity [1], so I'm skipping those.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102399

Change-Id: I75a2443dc71e6b80613b8edd52a04d3379355728
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2021-09-18 11:29:23 +02:00
parent 7eeda336b4
commit c578d9943a
2 changed files with 4 additions and 4 deletions

View File

@ -235,7 +235,7 @@ public:
};
template <typename Mutex>
class QMutexLocker
class [[nodiscard]] QMutexLocker
{
public:
inline explicit QMutexLocker(Mutex *mutex) QT_MUTEX_LOCK_NOEXCEPT
@ -313,7 +313,7 @@ private:
class QRecursiveMutex : public QMutex {};
template <typename Mutex>
class QMutexLocker
class [[nodiscard]] QMutexLocker
{
public:
inline explicit QMutexLocker(Mutex *) noexcept {}

View File

@ -103,7 +103,7 @@ typedef QScopedPointerObjectDeleteLater<QObject> QScopedPointerDeleteLater;
#endif
template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
class QScopedPointer
class [[nodiscard]] QScopedPointer
{
public:
explicit QScopedPointer(T *p = nullptr) noexcept : d(p)
@ -223,7 +223,7 @@ private:
};
template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
class [[nodiscard]] QScopedArrayPointer : public QScopedPointer<T, Cleanup>
{
template <typename Ptr>
using if_same_type = typename std::enable_if<std::is_same<typename std::remove_cv<T>::type, Ptr>::value, bool>::type;