Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-63352

Useless casts in MOC generated files

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.9.2
    • 5.9.1
    • Build tools: moc
    • None
    • Windows 10 x64
      Qt 5.9.1 x64 from Msys2
      GCC (G++) 7.2 x64 from Msys2
      CMake 3.9.2 x64 from Msys2
      Qt Creator 4.4.0

    Description

      Steps to reproduce:
      1. Open the attached simple project where a class derives from QObject and has Q_OBJECT macro inside its body.
      2. Compile it with GCC (G++) with -Wuseless-cast (already in CMakeLists.txt).

      Expected result:
      Successful compilation with no warnings or errors.

      Actual result:
      The compiler displays a warning in the MOC generated function
          Foo::qt_metacast(const char *_clname)
      in line
          return static_cast<void*>(const_cast< Foo*>(this));

      Full warning:

      D:\dev\project\test\useless-cast-test\main.cpp:13: In file included from D:/dev/project/test/useless-cast-test/main.cpp:13:0:
      D:\dev\project\test\build-useless-cast-test-Qt_5_9_1_MinGW_x64_GCC_7_2-Debug\useless-cast-test_autogen\include\main.moc:-1: In member function 'virtual void* Foo::qt_metacast(const char*)':
      D:\dev\project\test\build-useless-cast-test-Qt_5_9_1_MinGW_x64_GCC_7_2-Debug\useless-cast-test_autogen\include\main.moc:79: warning: useless cast to type 'Foo*' [-Wuseless-cast]
      return static_cast<void*>(const_cast< Foo*>(this));
                                                                                    ^

      The problem is worse when you compile with -Werror to treat every warning as error, because then it's not obvious how to fix this issue.

      Attachments

        1. CMakeLists.txt
          0.3 kB
        2. main.cpp
          0.1 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ogoffart Olivier Goffart (Woboq GmbH)
            fazer Arkadiusz Piekarz
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes