From 2422933f6d81efffd70793be96eb5d453cdff1a8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 27 Jun 2022 07:24:47 +0200 Subject: [PATCH] moc: fix GCC -Wuseless-cast warnings When being queried for QMetaObject::IndexOfMethod, qt_static_metacall would compare the argument with each of the class' methods' addresses. Taking the address of an overloaded function is ambiguous unless you cast to the right type, which the moc-generated code did using explicit static_cast<>s. If the function is not overloaded, GCC would warn about the static_cast<> being "useless", which isn't wrong. Fix by using an implicit cast to a local variable of the correct type instead of an explicit cast. Since there's no explicit cast anymore, GCC doesn't warn. Code before the change: using _t = void(Counter::*)(int ); if (*reinterpret_cast<_t*>(_a[1]) == static_cast<_t>(&Counter::valueChanged)) { ^-------------------------------------- -Wuseless-cast *result = 0; return; } After: using _t = void(Counter::*)(int ); if (_t _q_method = &Counter::valueChanged; *reinterpret_cast<_t*>(_a[1]) == _q_method) { *result = 0; return; } Since we're using a C++17 construct, we can't pick to 5.15. Fixes: QTBUG-71938 Change-Id: If6ba4bf17b3bf7f64e9662ba9d085273882fb460 Reviewed-by: Mate Barany Reviewed-by: Fabian Kosmale Reviewed-by: Thiago Macieira (cherry picked from commit 20a1526a23d23c32eca2bfd9b3194e30771902ac) Reviewed-by: Qt Cherry-pick Bot --- src/tools/moc/generator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index 03d7204534b..957216a77d6 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -1204,7 +1204,7 @@ void Generator::generateStaticMetacall() fprintf(out, ") const;\n"); else fprintf(out, ");\n"); - fprintf(out, " if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&%s::%s)) {\n", + fprintf(out, " if (_t _q_method = &%s::%s; *reinterpret_cast<_t *>(_a[1]) == _q_method) {\n", cdef->classname.constData(), f.name.constData()); fprintf(out, " *result = %d;\n", methodindex); fprintf(out, " return;\n");