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 <mate.barany@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 20a1526a23d23c32eca2bfd9b3194e30771902ac)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-06-27 07:24:47 +02:00 committed by Qt Cherry-pick Bot
parent 9e092ec83b
commit afa7f494eb

View File

@ -1186,7 +1186,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");