Skip to content

Commit

Permalink
tidy: Use dynamic_cast when casting to a derived class.
Browse files Browse the repository at this point in the history
The clang-tidy "static cast downcast" checker pointed out a few places
where data structures were statically cast from a base class to a
derived class.  Many of these places do check the type before casting,
but to be completely safe they should by dynamically cast and the
casted value tested against the nullptr.

https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-static-cast-downcast.html
  • Loading branch information
linuxdude42 committed Dec 9, 2019
1 parent 6975d56 commit ecbdbaa
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .clang-tidy
Expand Up @@ -104,6 +104,8 @@ Checks: '-*,
-cppcoreguidelines-pro-type-cstyle-cast,
-cppcoreguidelines-pro-type-member-init,
-cppcoreguidelines-pro-type-reinterpret-cast,
# The qapplication.h header file includes a static_cast.
-cppcoreguidelines-pro-type-static-cast-downcast,
# Recommends using boost:: to access unions. ,
Expand Down
45 changes: 32 additions & 13 deletions mythtv/libs/libmythui/mythmainwindow.cpp
Expand Up @@ -2138,6 +2138,7 @@ bool MythMainWindow::eventFilter(QObject * /*watched*/, QEvent *e)

// Work around weird GCC run-time bug. Only manifest on Mac OS X
if (!ke)
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
ke = static_cast<QKeyEvent *>(e);

#ifdef Q_OS_LINUX
Expand Down Expand Up @@ -2354,7 +2355,9 @@ bool MythMainWindow::eventFilter(QObject * /*watched*/, QEvent *e)
{
ResetIdleTimer();
ShowMouseCursor(true);
auto* qmw = static_cast<QWheelEvent*>(e);
auto* qmw = dynamic_cast<QWheelEvent*>(e);
if (qmw == nullptr)
return false;
int delta = qmw->delta();
if (delta>0)
{
Expand Down Expand Up @@ -2391,7 +2394,9 @@ void MythMainWindow::customEvent(QEvent *ce)
{
if (ce->type() == MythGestureEvent::kEventType)
{
auto *ge = static_cast<MythGestureEvent*>(ce);
auto *ge = dynamic_cast<MythGestureEvent*>(ce);
if (ge == nullptr)
return;
MythScreenStack *toplevel = GetMainStack();
if (toplevel)
{
Expand All @@ -2408,7 +2413,9 @@ void MythMainWindow::customEvent(QEvent *ce)
}
else if (ce->type() == ExternalKeycodeEvent::kEventType)
{
auto *eke = static_cast<ExternalKeycodeEvent *>(ce);
auto *eke = dynamic_cast<ExternalKeycodeEvent *>(ce);
if (eke == nullptr)
return;
int keycode = eke->getKeycode();

QKeyEvent key(QEvent::KeyPress, keycode, Qt::NoModifier);
Expand All @@ -2423,7 +2430,9 @@ void MythMainWindow::customEvent(QEvent *ce)
else if (ce->type() == LircKeycodeEvent::kEventType &&
!d->m_ignoreLircKeys)
{
auto *lke = static_cast<LircKeycodeEvent *>(ce);
auto *lke = dynamic_cast<LircKeycodeEvent *>(ce);
if (lke == nullptr)
return;

if (LircKeycodeEvent::kLIRCInvalidKeyCombo == lke->modifiers())
{
Expand Down Expand Up @@ -2453,9 +2462,11 @@ void MythMainWindow::customEvent(QEvent *ce)
else if (ce->type() == JoystickKeycodeEvent::kEventType &&
!d->m_ignoreJoystickKeys)
{
auto *jke = static_cast<JoystickKeycodeEvent *>(ce);
int keycode = jke->getKeycode();
auto *jke = dynamic_cast<JoystickKeycodeEvent *>(ce);
if (jke == nullptr)
return;

int keycode = jke->getKeycode();
if (keycode)
{
MythUIHelper::ResetScreensaver();
Expand Down Expand Up @@ -2486,7 +2497,9 @@ void MythMainWindow::customEvent(QEvent *ce)
#endif
else if (ce->type() == MythMediaEvent::kEventType)
{
auto *me = static_cast<MythMediaEvent*>(ce);
auto *me = dynamic_cast<MythMediaEvent*>(ce);
if (me == nullptr)
return;

// A listener based system might be more efficient, but we should never
// have that many screens open at once so impact should be minimal.
Expand Down Expand Up @@ -2523,7 +2536,9 @@ void MythMainWindow::customEvent(QEvent *ce)
}
else if (ce->type() == ScreenSaverEvent::kEventType)
{
auto *sse = static_cast<ScreenSaverEvent *>(ce);
auto *sse = dynamic_cast<ScreenSaverEvent *>(ce);
if (sse == nullptr)
return;
switch (sse->getSSEventType())
{
case ScreenSaverEvent::ssetDisable:
Expand Down Expand Up @@ -2577,9 +2592,11 @@ void MythMainWindow::customEvent(QEvent *ce)
}
else if (ce->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(ce);
QString message = me->Message();
auto *me = dynamic_cast<MythEvent *>(ce);
if (me == nullptr)
return;

QString message = me->Message();
if (message.startsWith(ACTION_HANDLEMEDIA))
{
if (me->ExtraDataCount() == 1)
Expand Down Expand Up @@ -2667,9 +2684,11 @@ void MythMainWindow::customEvent(QEvent *ce)
}
else if (ce->type() == MythEvent::MythUserMessage)
{
auto *me = static_cast<MythEvent *>(ce);
const QString& message = me->Message();
auto *me = dynamic_cast<MythEvent *>(ce);
if (me == nullptr)
return;

const QString& message = me->Message();
if (!message.isEmpty())
ShowOkPopup(message);
}
Expand All @@ -2679,7 +2698,7 @@ void MythMainWindow::customEvent(QEvent *ce)
}
else if (ce->type() == MythCallbackEvent::kCallbackType)
{
auto *me = static_cast<MythCallbackEvent*>(ce);
auto *me = dynamic_cast<MythCallbackEvent*>(ce);
if (me && me->m_function)
me->m_function(me->m_opaque1, me->m_opaque2, me->m_opaque3);
}
Expand Down
22 changes: 17 additions & 5 deletions mythtv/libs/libmythui/mythuiwebbrowser.cpp
Expand Up @@ -244,7 +244,10 @@ void BrowserApi::customEvent(QEvent *e)
{
if (e->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(e);
auto *me = dynamic_cast<MythEvent *>(e);
if (me == nullptr)
return;

const QString& message = me->Message();

if (!message.startsWith("MUSIC_CONTROL"))
Expand Down Expand Up @@ -291,9 +294,16 @@ bool MythWebPage::extension(Extension extension, const ExtensionOption *option,
if (!option || !output)
return false;

const auto *erroroption
= static_cast<const ErrorPageExtensionOption *>(option);
// Using static_cast yields the clang-tidy warning: do not use
// static_cast to downcast from a base to a derived class; use
// dynamic_cast instead. Using dynamic-cast yields the
// compiler error: 'QWebPage::ExtensionOption' is not
// polymorphic.
//
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
const auto *erroroption = static_cast<const ErrorPageExtensionOption *>(option);
ErrorPageExtensionReturn *erroroutput = nullptr;
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
erroroutput = static_cast<ErrorPageExtensionReturn *>(output);

QString filename = "htmls/notfound.html";
Expand Down Expand Up @@ -640,9 +650,11 @@ void MythWebView::customEvent(QEvent *event)
}
else if (event->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(event);
QStringList tokens = me->Message().split(" ", QString::SkipEmptyParts);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;

QStringList tokens = me->Message().split(" ", QString::SkipEmptyParts);
if (tokens.isEmpty())
return;

Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythui/opengl/mythpainteropengl.cpp
Expand Up @@ -86,7 +86,7 @@ void MythOpenGLPainter::Begin(QPaintDevice *Parent)

if (!m_render)
{
auto* glwin = static_cast<MythPainterWindowGL*>(m_parent);
auto* glwin = dynamic_cast<MythPainterWindowGL*>(m_parent);
if (!glwin)
{
LOG(VB_GENERAL, LOG_ERR, "FATAL ERROR: Failed to cast parent to MythPainterWindowGL");
Expand Down
Expand Up @@ -57,7 +57,9 @@ void WebSocketMythEvent::customEvent(QEvent* event)
if (!m_sendEvents)
return;

auto *me = static_cast<MythEvent *>(event);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;
QString message = me->Message();

if (message.startsWith("SYSTEM_EVENT"))
Expand Down
4 changes: 3 additions & 1 deletion mythtv/programs/mythbackend/mainserver.cpp
Expand Up @@ -1130,7 +1130,9 @@ void MainServer::customEvent(QEvent *e)

if (e->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(e);
auto *me = dynamic_cast<MythEvent *>(e);
if (me == nullptr)
return;

QString message = me->Message();
QString error;
Expand Down
5 changes: 4 additions & 1 deletion mythtv/programs/mythcommflag/main.cpp
Expand Up @@ -469,7 +469,10 @@ static void incomingCustomEvent(QEvent* e)
{
if (e->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(e);
auto *me = dynamic_cast<MythEvent *>(e);
if (me == nullptr)
return;

QString message = me->Message();

message = message.simplified();
Expand Down
4 changes: 3 additions & 1 deletion mythtv/programs/mythfrontend/audiogeneralsettings.cpp
Expand Up @@ -100,7 +100,9 @@ void AudioConfigScreen::Init(void)
{
StandardSettingDialog::Init();

auto *settings = static_cast<AudioConfigSettings*>(GetGroupSettings());
auto *settings = dynamic_cast<AudioConfigSettings*>(GetGroupSettings());
if (settings == nullptr)
return;
settings->CheckConfiguration();
}

Expand Down
5 changes: 4 additions & 1 deletion mythtv/programs/mythfrontend/backendconnectionmanager.cpp
Expand Up @@ -65,7 +65,10 @@ void BackendConnectionManager::customEvent(QEvent *event)

if (event->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(event);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;

const QString& message = me->Message();

if (message == "BACKEND_SOCKETS_CLOSED")
Expand Down
5 changes: 4 additions & 1 deletion mythtv/programs/mythfrontend/galleryslideview.cpp
Expand Up @@ -212,7 +212,10 @@ void GallerySlideView::customEvent(QEvent *event)
{
if (event->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(event);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;

const QString& message = me->Message();

QStringList extra = me->ExtraDataList();
Expand Down
5 changes: 4 additions & 1 deletion mythtv/programs/mythfrontend/gallerythumbview.cpp
Expand Up @@ -351,7 +351,10 @@ void GalleryThumbView::customEvent(QEvent *event)

if (event->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(event);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;

const QString& mesg = me->Message();
QStringList extra = me->ExtraDataList();

Expand Down
9 changes: 6 additions & 3 deletions mythtv/programs/mythfrontend/guidegrid.cpp
Expand Up @@ -1846,7 +1846,10 @@ void GuideGrid::customEvent(QEvent *event)
{
if (event->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(event);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;

const QString& message = me->Message();

if (message == "SCHEDULE_CHANGE")
Expand Down Expand Up @@ -2004,8 +2007,8 @@ void GuideGrid::customEvent(QEvent *event)
}
else if (event->type() == UpdateGuideEvent::kEventType)
{
auto *uge = static_cast<UpdateGuideEvent*>(event);
if (uge->m_updater)
auto *uge = dynamic_cast<UpdateGuideEvent*>(event);
if (uge && uge->m_updater)
{
uge->m_updater->ExecuteUI();
delete uge->m_updater;
Expand Down
4 changes: 3 additions & 1 deletion mythtv/programs/mythfrontend/idlescreen.cpp
Expand Up @@ -244,7 +244,9 @@ void IdleScreen::customEvent(QEvent* event)
{
if (event->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(event);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;

if (me->Message().startsWith("RECONNECT_"))
{
Expand Down
26 changes: 17 additions & 9 deletions mythtv/programs/mythfrontend/networkcontrol.cpp
Expand Up @@ -435,7 +435,7 @@ void NetworkControl::receiveCommand(QString &command)
{
LOG(VB_NETWORK, LOG_INFO, LOC +
QString("NetworkControl::receiveCommand(%1)").arg(command));
auto *ncc = static_cast<NetworkControlClient *>(sender());
auto *ncc = dynamic_cast<NetworkControlClient *>(sender());
if (!ncc)
return;

Expand Down Expand Up @@ -1182,7 +1182,9 @@ QString NetworkControl::processTheme( NetworkCommand* nc)
if (!topScreen)
return QString("ERROR: no top screen found!");

auto *currType = static_cast<MythUIType*>(topScreen);
auto *currType = dynamic_cast<MythUIType*>(topScreen);
if (currType == nullptr)
return QString("ERROR: cannot cast top screen!");

while (!path.isEmpty())
{
Expand Down Expand Up @@ -1225,7 +1227,9 @@ QString NetworkControl::processTheme( NetworkCommand* nc)
if (!topScreen)
return QString("ERROR: no top screen found!");

auto *currType = static_cast<MythUIType*>(topScreen);
auto *currType = dynamic_cast<MythUIType*>(topScreen);
if (currType == nullptr)
return QString("ERROR: cannot cast top screen!");

while (path.count() > 1)
{
Expand Down Expand Up @@ -1270,11 +1274,10 @@ QString NetworkControl::processTheme( NetworkCommand* nc)
topScreen = stack->GetTopScreen();
}

auto *currType = dynamic_cast<MythUIType*>(topScreen);
if (!topScreen)
return QString("ERROR: no top screen found!");

auto *currType = static_cast<MythUIType*>(topScreen);

while (path.count() > 1)
{
QString childName = path.takeFirst();
Expand Down Expand Up @@ -1544,7 +1547,10 @@ void NetworkControl::customEvent(QEvent *e)
{
if (e->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(e);
auto *me = dynamic_cast<MythEvent *>(e);
if (me == nullptr)
return;

const QString& message = me->Message();

if (message.startsWith("MUSIC_CONTROL"))
Expand Down Expand Up @@ -1617,9 +1623,11 @@ void NetworkControl::customEvent(QEvent *e)
}
else if (e->type() == NetworkControlCloseEvent::kEventType)
{
auto *ncce = static_cast<NetworkControlCloseEvent*>(e);
NetworkControlClient *ncc = ncce->getClient();

auto *ncce = dynamic_cast<NetworkControlCloseEvent*>(e);
if (ncce == nullptr)
return;

NetworkControlClient *ncc = ncce->getClient();
deleteClient(ncc);
}
}
Expand Down
6 changes: 4 additions & 2 deletions mythtv/programs/mythfrontend/playbackbox.cpp
Expand Up @@ -3983,7 +3983,6 @@ void PlaybackBox::customEvent(QEvent *event)
if (event->type() == DialogCompletionEvent::kEventType)
{
auto *dce = dynamic_cast<DialogCompletionEvent*>(event);

if (!dce)
return;

Expand All @@ -3994,7 +3993,10 @@ void PlaybackBox::customEvent(QEvent *event)
}
else if (event->type() == MythEvent::MythEventMessage)
{
auto *me = static_cast<MythEvent *>(event);
auto *me = dynamic_cast<MythEvent *>(event);
if (me == nullptr)
return;

const QString& message = me->Message();

if (message.startsWith("RECORDING_LIST_CHANGE"))
Expand Down

0 comments on commit ecbdbaa

Please sign in to comment.