From 43af54c2282a433f53675604cf76b646ae241a1c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 3 Aug 2018 14:34:17 +0200 Subject: [PATCH] Windows QPA: Fix some clang-tidy-warnings - Replace index-based loops by range-based for - Change else if to if after return/break/continue or simplify - Fix indentation - Do not check for non-null before invoking delete on pointer - Use isEmpty() instead size() to check for empty containers - Remove C-style casts - Use raw string literal - Do not repeat type in return, use {} instead - Reference local variables by const ref where applicable Change-Id: I5cc4b4026a10bddb561ba1ba8ec137e0d4119f94 Reviewed-by: Miguel Costa Reviewed-by: Joerg Bornemann --- .../platforms/windows/qwindowscursor.cpp | 9 ++--- .../windows/qwindowsdialoghelpers.cpp | 10 +++--- .../platforms/windows/qwindowseglcontext.cpp | 12 +++---- .../platforms/windows/qwindowsglcontext.cpp | 10 ++---- .../platforms/windows/qwindowsintegration.cpp | 3 +- .../platforms/windows/qwindowskeymapper.cpp | 7 ++-- .../platforms/windows/qwindowsmime.cpp | 35 ++++++++++--------- .../windows/qwindowsmousehandler.cpp | 4 +-- .../windows/qwindowsnativeinterface.cpp | 6 ++-- src/plugins/platforms/windows/qwindowsole.cpp | 3 +- .../platforms/windows/qwindowsscreen.cpp | 5 +-- .../windows/qwindowstabletsupport.cpp | 2 +- .../platforms/windows/qwindowstheme.cpp | 8 ++--- .../platforms/windows/qwindowswindow.cpp | 12 ++----- .../platforms/windows/qwindowswindow.h | 2 +- 15 files changed, 59 insertions(+), 69 deletions(-) diff --git a/src/plugins/platforms/windows/qwindowscursor.cpp b/src/plugins/platforms/windows/qwindowscursor.cpp index 0924fb822e..1443e5e920 100644 --- a/src/plugins/platforms/windows/qwindowscursor.cpp +++ b/src/plugins/platforms/windows/qwindowscursor.cpp @@ -524,10 +524,11 @@ HCURSOR QWindowsCursor::createCursorFromShape(Qt::CursorShape cursorShape, const } // Load available standard cursors from resources - const QWindowsStandardCursorMapping *sEnd = standardCursors + sizeof(standardCursors) / sizeof(standardCursors[0]); - for (const QWindowsStandardCursorMapping *s = standardCursors; s < sEnd; ++s) { - if (s->shape == cursorShape) - return static_cast(LoadImage(0, s->resource, IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED)); + for (const QWindowsStandardCursorMapping &s : standardCursors) { + if (s.shape == cursorShape) { + return static_cast(LoadImage(nullptr, s.resource, IMAGE_CURSOR, + 0, 0, LR_DEFAULTSIZE | LR_SHARED)); + } } qWarning("%s: Invalid cursor shape %d", __FUNCTION__, cursorShape); diff --git a/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp b/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp index 40ab2a4dd2..635033b43f 100644 --- a/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp +++ b/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp @@ -364,7 +364,7 @@ static BOOL QT_WIN_CALLBACK findDialogEnumWindowsProc(HWND hwnd, LPARAM lParam) wchar_t buf[256]; if (!RealGetWindowClass(hwnd, buf, sizeof(buf)/sizeof(wchar_t)) || buf[0] != L'#') return TRUE; - if (!GetWindowTextW(hwnd, buf, sizeof(buf)/sizeof(wchar_t)) || wcscmp(buf, context->title.data())) + if (!GetWindowTextW(hwnd, buf, sizeof(buf)/sizeof(wchar_t)) || wcscmp(buf, context->title.data()) != 0) return TRUE; context->hwnd = hwnd; return FALSE; @@ -1545,8 +1545,8 @@ QWindowsNativeDialogBase *QWindowsFileDialogHelper::createNativeDialog() result->updateDirectory(); result->updateSelectedNameFilter(); const QList initialSelection = opts->initiallySelectedFiles(); - if (initialSelection.size() > 0) { - const QUrl url = initialSelection.front(); + if (!initialSelection.empty()) { + const QUrl &url = initialSelection.constFirst(); if (url.isLocalFile()) { QFileInfo info(url.toLocalFile()); if (!info.isDir()) @@ -1698,7 +1698,7 @@ void QWindowsXpNativeFileDialog::doExec(HWND owner) const QStringList nameFilters = m_options->nameFilters(); if (selectedFilterIndex >= 0 && selectedFilterIndex < nameFilters.size()) m_data.setSelectedNameFilter(nameFilters.at(selectedFilterIndex)); - QUrl firstFile = selectedFiles.front(); + const QUrl &firstFile = selectedFiles.constFirst(); m_data.setDirectory(firstFile.adjusted(QUrl::RemoveFilename)); m_result = QPlatformDialogHelper::Accepted; emit accepted(); @@ -1727,7 +1727,7 @@ int QWindowsXpNativeFileDialog::existingDirCallback(HWND hwnd, UINT uMsg, LPARAM switch (uMsg) { case BFFM_INITIALIZED: { if (!m_title.isEmpty()) - SetWindowText(hwnd, (wchar_t *)m_title.utf16()); + SetWindowText(hwnd, reinterpret_cast(m_title.utf16())); const QString initialFile = QDir::toNativeSeparators(m_data.directory().toLocalFile()); if (!initialFile.isEmpty()) SendMessage(hwnd, BFFM_SETSELECTION, TRUE, LPARAM(initialFile.utf16())); diff --git a/src/plugins/platforms/windows/qwindowseglcontext.cpp b/src/plugins/platforms/windows/qwindowseglcontext.cpp index a8d0a42bdb..52f3c56beb 100644 --- a/src/plugins/platforms/windows/qwindowseglcontext.cpp +++ b/src/plugins/platforms/windows/qwindowseglcontext.cpp @@ -889,19 +889,19 @@ EGLConfig QWindowsEGLContext::chooseConfig(const QSurfaceFormat &format) EGLint green = 0; EGLint blue = 0; EGLint alpha = 0; - for (int i = 0; i < configs.size(); ++i) { + for (const EGLConfig &config : configs) { if (confAttrRed) - QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, configs[i], EGL_RED_SIZE, &red); + QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, config, EGL_RED_SIZE, &red); if (confAttrGreen) - QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, configs[i], EGL_GREEN_SIZE, &green); + QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &green); if (confAttrBlue) - QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, configs[i], EGL_BLUE_SIZE, &blue); + QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &blue); if (confAttrAlpha) - QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, configs[i], EGL_ALPHA_SIZE, &alpha); + QWindowsEGLStaticContext::libEGL.eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &alpha); if (red == confAttrRed && green == confAttrGreen && blue == confAttrBlue && alpha == confAttrAlpha) - return configs[i]; + return config; } } while (reduceConfigAttributes(&configureAttributes)); diff --git a/src/plugins/platforms/windows/qwindowsglcontext.cpp b/src/plugins/platforms/windows/qwindowsglcontext.cpp index 2409d70ec9..851a6c961e 100644 --- a/src/plugins/platforms/windows/qwindowsglcontext.cpp +++ b/src/plugins/platforms/windows/qwindowsglcontext.cpp @@ -206,18 +206,12 @@ bool QWindowsOpengl32DLL::init(bool softwareRendering) BOOL QWindowsOpengl32DLL::swapBuffers(HDC dc) { - if (moduleIsNotOpengl32()) - return wglSwapBuffers(dc); - else - return SwapBuffers(dc); + return moduleIsNotOpengl32() ? wglSwapBuffers(dc) : SwapBuffers(dc); } BOOL QWindowsOpengl32DLL::setPixelFormat(HDC dc, int pf, const PIXELFORMATDESCRIPTOR *pfd) { - if (moduleIsNotOpengl32()) - return wglSetPixelFormat(dc, pf, pfd); - else - return SetPixelFormat(dc, pf, pfd); + return moduleIsNotOpengl32() ? wglSetPixelFormat(dc, pf, pfd) : SetPixelFormat(dc, pf, pfd); } QWindowsOpenGLContext *QOpenGLStaticContext::createContext(QOpenGLContext *context) diff --git a/src/plugins/platforms/windows/qwindowsintegration.cpp b/src/plugins/platforms/windows/qwindowsintegration.cpp index 7194fa1d1f..4c31808c2d 100644 --- a/src/plugins/platforms/windows/qwindowsintegration.cpp +++ b/src/plugins/platforms/windows/qwindowsintegration.cpp @@ -255,8 +255,7 @@ QWindowsIntegrationPrivate::QWindowsIntegrationPrivate(const QStringList ¶mL QWindowsIntegrationPrivate::~QWindowsIntegrationPrivate() { - if (m_fontDatabase) - delete m_fontDatabase; + delete m_fontDatabase; } QWindowsIntegration *QWindowsIntegration::m_instance = nullptr; diff --git a/src/plugins/platforms/windows/qwindowskeymapper.cpp b/src/plugins/platforms/windows/qwindowskeymapper.cpp index 48941d78ca..a2b5573f78 100644 --- a/src/plugins/platforms/windows/qwindowskeymapper.cpp +++ b/src/plugins/platforms/windows/qwindowskeymapper.cpp @@ -653,8 +653,8 @@ static inline int asciiToKeycode(char a, int state) void QWindowsKeyMapper::deleteLayouts() { - for (size_t i = 0; i < NumKeyboardLayoutItems; ++i) - keyLayout[i].exists = false; + for (KeyboardLayoutItem &k : keyLayout) + k.exists = false; } void QWindowsKeyMapper::changeKeyboard() @@ -1111,7 +1111,8 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, const MSG &ms if (uch.isHighSurrogate()) { m_lastHighSurrogate = uch; return true; - } else if (uch.isLowSurrogate() && !m_lastHighSurrogate.isNull()) { + } + if (uch.isLowSurrogate() && !m_lastHighSurrogate.isNull()) { if (QObject *focusObject = QGuiApplication::focusObject()) { const QChar chars[2] = {m_lastHighSurrogate, uch}; QInputMethodEvent event; diff --git a/src/plugins/platforms/windows/qwindowsmime.cpp b/src/plugins/platforms/windows/qwindowsmime.cpp index 0cfd79f451..98fccb118d 100644 --- a/src/plugins/platforms/windows/qwindowsmime.cpp +++ b/src/plugins/platforms/windows/qwindowsmime.cpp @@ -628,7 +628,8 @@ bool QWindowsMimeText::convertFromMime(const FORMATETC &formatetc, const QMimeDa } o[j]=0; return setData(r, pmedium); - } else if (cf == CF_UNICODETEXT) { + } + if (cf == CF_UNICODETEXT) { QString str = mimeData->text(); const QChar *u = str.unicode(); QString res; @@ -750,9 +751,9 @@ QWindowsMimeURI::QWindowsMimeURI() bool QWindowsMimeURI::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const { if (mimeData->hasUrls() && getCf(formatetc) == CF_HDROP) { - QList urls = mimeData->urls(); - for (int i=0; i urls = mimeData->urls(); + for (const QUrl &url : urls) { + if (url.isLocalFile()) return true; } } @@ -763,11 +764,11 @@ bool QWindowsMimeURI::convertFromMime(const FORMATETC &formatetc, const QMimeDat { if (canConvertFromMime(formatetc, mimeData)) { if (getCf(formatetc) == CF_HDROP) { - QList urls = mimeData->urls(); + const QList &urls = mimeData->urls(); QStringList fileNames; int size = sizeof(DROPFILES)+2; - for (int i=0; i urls = mimeData->urls(); QByteArray result; if (!urls.isEmpty()) { @@ -803,7 +805,8 @@ bool QWindowsMimeURI::convertFromMime(const FORMATETC &formatetc, const QMimeDat result.append('\0'); result.append('\0'); return setData(result, pmedium); - } else if (getCf(formatetc) == CF_INETURL) { + } + if (getCf(formatetc) == CF_INETURL) { QList urls = mimeData->urls(); QByteArray result; if (!urls.isEmpty()) @@ -873,7 +876,7 @@ QVariant QWindowsMimeURI::convertToMime(const QString &mimeType, LPDATAOBJECT pD if (preferredType == QVariant::Url && urls.size() == 1) return urls.at(0); - else if (!urls.isEmpty()) + if (!urls.isEmpty()) return urls; } else if (canGetData(CF_INETURL_W, pDataObj)) { QByteArray data = getData(CF_INETURL_W, pDataObj); @@ -1076,10 +1079,8 @@ QString QWindowsMimeImage::mimeForFormat(const FORMATETC &formatetc) const bool QWindowsMimeImage::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const { - if (mimeType == QLatin1String("application/x-qt-image") && - (canGetData(CF_DIB, pDataObj) || canGetData(CF_PNG, pDataObj))) - return true; - return false; + return mimeType == QLatin1String("application/x-qt-image") + && (canGetData(CF_DIB, pDataObj) || canGetData(CF_PNG, pDataObj)); } bool QWindowsMimeImage::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const @@ -1135,7 +1136,7 @@ bool QWindowsMimeImage::hasOriginalDIBV5(IDataObject *pDataObj) const CoTaskMemFree(fc.ptd); if (fc.cfFormat == CF_DIB) break; - else if (fc.cfFormat == CF_DIBV5) { + if (fc.cfFormat == CF_DIBV5) { isSynthesized = false; break; } @@ -1329,7 +1330,7 @@ QStringList QLastResortMimes::excludeList; QLastResortMimes::QLastResortMimes() { //MIME Media-Types - if (!ianaTypes.size()) { + if (ianaTypes.isEmpty()) { ianaTypes.append(QStringLiteral("application/")); ianaTypes.append(QStringLiteral("audio/")); ianaTypes.append(QStringLiteral("example/")); @@ -1341,7 +1342,7 @@ QLastResortMimes::QLastResortMimes() ianaTypes.append(QStringLiteral("video/")); } //Types handled by other classes - if (!excludeList.size()) { + if (excludeList.isEmpty()) { excludeList.append(QStringLiteral("HTML Format")); excludeList.append(QStringLiteral("UniformResourceLocator")); excludeList.append(QStringLiteral("text/html")); diff --git a/src/plugins/platforms/windows/qwindowsmousehandler.cpp b/src/plugins/platforms/windows/qwindowsmousehandler.cpp index 92aaf70e40..87f48e0c84 100644 --- a/src/plugins/platforms/windows/qwindowsmousehandler.cpp +++ b/src/plugins/platforms/windows/qwindowsmousehandler.cpp @@ -363,9 +363,9 @@ bool QWindowsMouseHandler::translateMouseEvent(QWindow *window, HWND hwnd, } m_previousCaptureWindow = window; return true; - } else if (m_leftButtonDown && !actualLeftDown) { - m_leftButtonDown = false; } + if (m_leftButtonDown && !actualLeftDown) + m_leftButtonDown = false; } // In this context, neither an invisible nor a transparent window (transparent regarding mouse diff --git a/src/plugins/platforms/windows/qwindowsnativeinterface.cpp b/src/plugins/platforms/windows/qwindowsnativeinterface.cpp index da6f13d01a..de11356fd4 100644 --- a/src/plugins/platforms/windows/qwindowsnativeinterface.cpp +++ b/src/plugins/platforms/windows/qwindowsnativeinterface.cpp @@ -275,11 +275,11 @@ QFunctionPointer QWindowsNativeInterface::platformFunction(const QByteArray &fun { if (function == QWindowsWindowFunctions::setTouchWindowTouchTypeIdentifier()) return QFunctionPointer(QWindowsWindow::setTouchWindowTouchTypeStatic); - else if (function == QWindowsWindowFunctions::setHasBorderInFullScreenIdentifier()) + if (function == QWindowsWindowFunctions::setHasBorderInFullScreenIdentifier()) return QFunctionPointer(QWindowsWindow::setHasBorderInFullScreenStatic); - else if (function == QWindowsWindowFunctions::setWindowActivationBehaviorIdentifier()) + if (function == QWindowsWindowFunctions::setWindowActivationBehaviorIdentifier()) return QFunctionPointer(QWindowsNativeInterface::setWindowActivationBehavior); - else if (function == QWindowsWindowFunctions::isTabletModeIdentifier()) + if (function == QWindowsWindowFunctions::isTabletModeIdentifier()) return QFunctionPointer(QWindowsNativeInterface::isTabletMode); return nullptr; } diff --git a/src/plugins/platforms/windows/qwindowsole.cpp b/src/plugins/platforms/windows/qwindowsole.cpp index 3e62bbdeca..5ecdf46cea 100644 --- a/src/plugins/platforms/windows/qwindowsole.cpp +++ b/src/plugins/platforms/windows/qwindowsole.cpp @@ -365,10 +365,9 @@ QWindowsOleEnumFmtEtc::Clone(LPENUMFORMATETC FAR* newEnum) if (result->isNull()) { delete result; return ResultFromScode(E_OUTOFMEMORY); - } else { - *newEnum = result; } + *newEnum = result; return NOERROR; } diff --git a/src/plugins/platforms/windows/qwindowsscreen.cpp b/src/plugins/platforms/windows/qwindowsscreen.cpp index bf7f359aac..29165ef72c 100644 --- a/src/plugins/platforms/windows/qwindowsscreen.cpp +++ b/src/plugins/platforms/windows/qwindowsscreen.cpp @@ -69,7 +69,7 @@ static inline QDpi monitorDPI(HMONITOR hMonitor) if (SUCCEEDED(QWindowsContext::shcoredll.getDpiForMonitor(hMonitor, 0, &dpiX, &dpiY))) return QDpi(dpiX, dpiY); } - return QDpi(0, 0); + return {0, 0}; } typedef QList WindowsScreenDataList; @@ -401,7 +401,8 @@ QPlatformScreen::SubpixelAntialiasingType QWindowsScreen::subpixelAntialiasingTy { QPlatformScreen::SubpixelAntialiasingType type = QPlatformScreen::subpixelAntialiasingTypeHint(); if (type == QPlatformScreen::Subpixel_None) { - QSettings settings(QLatin1String("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Avalon.Graphics\\DISPLAY1"), QSettings::NativeFormat); + QSettings settings(QLatin1String(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Avalon.Graphics\DISPLAY1)"), + QSettings::NativeFormat); int registryValue = settings.value(QLatin1String("PixelStructure"), -1).toInt(); switch (registryValue) { case 0: diff --git a/src/plugins/platforms/windows/qwindowstabletsupport.cpp b/src/plugins/platforms/windows/qwindowstabletsupport.cpp index 2ab59b11ca..fa209f09c4 100644 --- a/src/plugins/platforms/windows/qwindowstabletsupport.cpp +++ b/src/plugins/platforms/windows/qwindowstabletsupport.cpp @@ -108,7 +108,7 @@ inline QPointF QWindowsTabletDeviceData::scaleCoordinates(int coordX, int coordY ((coordY - minY) * qAbs(targetHeight) / qAbs(qreal(maxY - minY))) + targetY : ((qAbs(maxY) - (coordY - minY)) * qAbs(targetHeight) / qAbs(qreal(maxY - minY))) + targetY; - return QPointF(x, y); + return {x, y}; } template diff --git a/src/plugins/platforms/windows/qwindowstheme.cpp b/src/plugins/platforms/windows/qwindowstheme.cpp index 9d2d4c28e0..d01a7b0a8a 100644 --- a/src/plugins/platforms/windows/qwindowstheme.cpp +++ b/src/plugins/platforms/windows/qwindowstheme.cpp @@ -107,7 +107,7 @@ static inline bool booleanSystemParametersInfo(UINT what, bool defaultValue) { BOOL result; if (SystemParametersInfo(what, 0, &result, 0)) - return result ? true : false; + return result != FALSE; return defaultValue; } @@ -121,9 +121,9 @@ static inline DWORD dWordSystemParametersInfo(UINT what, DWORD defaultValue) static inline QColor mixColors(const QColor &c1, const QColor &c2) { - return QColor ((c1.red() + c2.red()) / 2, - (c1.green() + c2.green()) / 2, - (c1.blue() + c2.blue()) / 2); + return {(c1.red() + c2.red()) / 2, + (c1.green() + c2.green()) / 2, + (c1.blue() + c2.blue()) / 2}; } static inline QColor getSysColor(int index) diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp index 150ab5fc04..17fc120b42 100644 --- a/src/plugins/platforms/windows/qwindowswindow.cpp +++ b/src/plugins/platforms/windows/qwindowswindow.cpp @@ -660,7 +660,7 @@ QWindowsWindowData WindowData result; result.flags = flags; - const HINSTANCE appinst = (HINSTANCE)GetModuleHandle(0); + const auto appinst = reinterpret_cast(GetModuleHandle(nullptr)); const QString windowClassName = QWindowsContext::instance()->registerWindowClass(w); @@ -1374,18 +1374,12 @@ bool QWindowsWindow::isEmbedded() const QPoint QWindowsWindow::mapToGlobal(const QPoint &pos) const { - if (m_data.hwnd) - return QWindowsGeometryHint::mapToGlobal(m_data.hwnd, pos); - else - return pos; + return m_data.hwnd ? QWindowsGeometryHint::mapToGlobal(m_data.hwnd, pos) : pos; } QPoint QWindowsWindow::mapFromGlobal(const QPoint &pos) const { - if (m_data.hwnd) - return QWindowsGeometryHint::mapFromGlobal(m_data.hwnd, pos); - else - return pos; + return m_data.hwnd ? QWindowsGeometryHint::mapFromGlobal(m_data.hwnd, pos) : pos; } static inline HWND transientParentHwnd(HWND hwnd) diff --git a/src/plugins/platforms/windows/qwindowswindow.h b/src/plugins/platforms/windows/qwindowswindow.h index 5ff25eb474..8f030f1917 100644 --- a/src/plugins/platforms/windows/qwindowswindow.h +++ b/src/plugins/platforms/windows/qwindowswindow.h @@ -430,7 +430,7 @@ inline QWindowsWindow *QWindowsWindow::windowsWindowOf(const QWindow *w) void *QWindowsWindow::userDataOf(HWND hwnd) { - return (void *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + return reinterpret_cast(GetWindowLongPtr(hwnd, GWLP_USERDATA)); } void QWindowsWindow::setUserDataOf(HWND hwnd, void *ud)