From 3fda04644a4d036f4227fe97cb58ababdafcc9f9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 4 May 2023 11:17:22 +0200 Subject: [PATCH] QRunnable: Extract Method warnNullCallable() The warning doesn't depend on the template argument, so factor it into an out-of-line Q_DECL_COLD_FUNCTION. This will automatically mark the is_null check as [[unlikely]], too. As a drive-by, use printf-style qWarning() (expands to less code and doesn't require #include ). Change-Id: I9ac2d912eb1d4f3d1d9b41d77700bed6d6850b0a Reviewed-by: Allan Sandfeld Jensen --- src/corelib/thread/qrunnable.cpp | 10 ++++++++++ src/corelib/thread/qrunnable.h | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/corelib/thread/qrunnable.cpp b/src/corelib/thread/qrunnable.cpp index 068dc1af2a1..684593e16b8 100644 --- a/src/corelib/thread/qrunnable.cpp +++ b/src/corelib/thread/qrunnable.cpp @@ -9,6 +9,16 @@ QRunnable::~QRunnable() { } +/*! + \internal + Prints a warning and returns nullptr. +*/ +QRunnable *QRunnable::warnNullCallable() +{ + qWarning("Trying to create null QRunnable. This may stop working."); + return nullptr; +} + /*! \class QRunnable \inmodule QtCore diff --git a/src/corelib/thread/qrunnable.h b/src/corelib/thread/qrunnable.h index 3b25b678363..37a99ea8413 100644 --- a/src/corelib/thread/qrunnable.h +++ b/src/corelib/thread/qrunnable.h @@ -5,7 +5,6 @@ #define QRUNNABLE_H #include -#include #include #include @@ -64,6 +63,9 @@ protected: { } }; + +private: + static Q_DECL_COLD_FUNCTION QRunnable *warnNullCallable(); }; class QGenericRunnable : public QRunnable @@ -105,10 +107,8 @@ QRunnable *QRunnable::create(Callable &&functionToRun) const void *functionPtr = reinterpret_cast(functionToRun); is_null = !functionPtr; } - if (is_null) { - qWarning() << "Trying to create null QRunnable. This may stop working."; - return nullptr; - } + if (is_null) + return warnNullCallable(); return new QGenericRunnable( new QGenericRunnableHelper>(