From 343fa701ed8cf28f8413901b804b312dd72438bb Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 22 Jun 2023 09:53:23 +0200 Subject: [PATCH] Pacify MSVC compiler incorrectly warning about unused variable Under some circumstances, MSVC seems to complain about SlotArgumentCount being unused qobject.h(210): warning C4189: 'SlotArgumentCount': local variable is initialized but not referenced note: see reference to function template instantiation 'QMetaObject::Connection QObject::connect>(const QAction *,Func1, const QtPrivate::ContextTypeForFunctor,void>::ContextType *, Func2 &&,Qt::ConnectionType)' being compiled This is nonsense, as SlotArgumentCount is used in the next line, to construct the list of signal arguments, but the workaround to declare the variable as [[maybe_unused]] is trivial. Add a connect statement to the test case that creates such a connection. This does not produce any warning with or without the attribute (and if it did, the build would fail for CI configuratinos setting -Werror). Fixes: QTBUG-114781 Change-Id: I4ee6f7d57c2836ef3dd9741d037d48181af2cdec Reviewed-by: Marc Mutz (cherry picked from commit 2b9ef2eb44c084d39ef8324cfe1ae42a98b3038f) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qobject.h | 1 + tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 49435b97016..182edaf7f2c 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -207,6 +207,7 @@ public: "Return type of the slot is not compatible with the return type of the signal."); } else { constexpr int FunctorArgumentCount = QtPrivate::ComputeFunctorArgumentCount, typename SignalType::Arguments>::Value; + [[maybe_unused]] constexpr int SlotArgumentCount = (FunctorArgumentCount >= 0) ? FunctorArgumentCount : 0; typedef typename QtPrivate::FunctorReturnType, typename QtPrivate::List_Left::Value>::Value SlotReturnType; diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 73b77932ce4..31345cc3333 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -6075,6 +6075,7 @@ void tst_QObject::connectFunctorArgDifference() connect(&timer, &QTimer::timeout, [=](){}); connect(&timer, &QTimer::objectNameChanged, [=](const QString &){}); + connect(&timer, &QTimer::objectNameChanged, this, [](){}); connect(qApp, &QCoreApplication::aboutToQuit, [=](){}); connect(&timer, &QTimer::objectNameChanged, [=](){});