From 74cecb1b6c3c59de76de3dd7ba4e61bec63e8feb Mon Sep 17 00:00:00 2001 From: Eugene Shalygin Date: Mon, 29 May 2017 14:10:31 +0200 Subject: [PATCH] Replace C-style casts with C++ ones --- cmake/Modules/MacroQbtCompilerSettings.cmake | 2 +- src/app/stacktrace.h | 2 +- src/base/bittorrent/infohash.cpp | 4 ++-- .../bittorrent/private/filterparserthread.cpp | 16 ++++++++-------- src/base/bittorrent/torrenthandle.cpp | 2 +- src/base/filesystemwatcher.cpp | 4 +++- src/base/net/private/geoipdatabase.cpp | 4 ++-- src/base/preferences.cpp | 4 ++-- src/base/utils/random.cpp | 2 +- src/gui/mainwindow.cpp | 2 +- src/gui/messageboxraised.cpp | 2 +- src/gui/optionsdlg.cpp | 6 +++--- src/gui/powermanagement/powermanagement_x11.cpp | 4 ++-- src/gui/previewlistdelegate.h | 2 +- src/gui/properties/pieceavailabilitybar.cpp | 4 ++-- src/gui/statsdialog.cpp | 2 +- src/gui/torrentcontentmodel.cpp | 4 ++-- src/gui/transferlistdelegate.cpp | 2 +- src/gui/transferlistwidget.cpp | 4 ++-- src/webui/btjson.cpp | 8 ++++---- 20 files changed, 41 insertions(+), 39 deletions(-) diff --git a/cmake/Modules/MacroQbtCompilerSettings.cmake b/cmake/Modules/MacroQbtCompilerSettings.cmake index 783457a69..114681c12 100644 --- a/cmake/Modules/MacroQbtCompilerSettings.cmake +++ b/cmake/Modules/MacroQbtCompilerSettings.cmake @@ -22,7 +22,7 @@ macro(qbt_set_compiler_options) # "-Weffc++" "-Werror -Wno-error=cpp" # we should modify code to make these ones obsolete - "-Wno-error=old-style-cast -Wno-error=sign-conversion -Wno-error=float-equal" + "-Wno-error=sign-conversion -Wno-error=float-equal" ) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) diff --git a/src/app/stacktrace.h b/src/app/stacktrace.h index bb9fb0f05..4bcccf859 100644 --- a/src/app/stacktrace.h +++ b/src/app/stacktrace.h @@ -33,7 +33,7 @@ static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames // allocate string which will be filled with the demangled function name size_t funcnamesize = 256; - char *funcname = (char *)malloc(funcnamesize); + char *funcname = static_cast(malloc(funcnamesize)); int functionNamesFound = 0; // iterate over the returned symbol lines. skip the first, it is the diff --git a/src/base/bittorrent/infohash.cpp b/src/base/bittorrent/infohash.cpp index 293e650fe..0e37c7509 100644 --- a/src/base/bittorrent/infohash.cpp +++ b/src/base/bittorrent/infohash.cpp @@ -41,7 +41,7 @@ InfoHash::InfoHash(const libtorrent::sha1_hash &nativeHash) , m_nativeHash(nativeHash) { char out[(libtorrent::sha1_hash::size * 2) + 1]; - libtorrent::to_hex((char const*)&m_nativeHash[0], libtorrent::sha1_hash::size, out); + libtorrent::to_hex(reinterpret_cast(&m_nativeHash[0]), libtorrent::sha1_hash::size, out); m_hashString = QString(out); } @@ -51,7 +51,7 @@ InfoHash::InfoHash(const QString &hashString) { QByteArray raw = m_hashString.toLatin1(); if (raw.size() == 40) - m_valid = libtorrent::from_hex(raw.constData(), 40, (char*)&m_nativeHash[0]); + m_valid = libtorrent::from_hex(raw.constData(), 40, reinterpret_cast(&m_nativeHash[0])); } diff --git a/src/base/bittorrent/private/filterparserthread.cpp b/src/base/bittorrent/private/filterparserthread.cpp index 6b93da05c..a3304815e 100644 --- a/src/base/bittorrent/private/filterparserthread.cpp +++ b/src/base/bittorrent/private/filterparserthread.cpp @@ -417,7 +417,7 @@ int FilterParserThread::parseP2BFilterFile() unsigned char version; if (!stream.readRawData(buf, sizeof(buf)) || memcmp(buf, "\xFF\xFF\xFF\xFFP2B", 7) - || !stream.readRawData((char*)&version, sizeof(version))) { + || !stream.readRawData(reinterpret_cast(&version), sizeof(version))) { LogMsg(tr("Parsing Error: The filter file is not a valid PeerGuardian P2B file."), Log::CRITICAL); return ruleCount; } @@ -428,8 +428,8 @@ int FilterParserThread::parseP2BFilterFile() std::string name; while(getlineInStream(stream, name, '\0') && !m_abort) { - if (!stream.readRawData((char*)&start, sizeof(start)) - || !stream.readRawData((char*)&end, sizeof(end))) { + if (!stream.readRawData(reinterpret_cast(&start), sizeof(start)) + || !stream.readRawData(reinterpret_cast(&end), sizeof(end))) { LogMsg(tr("Parsing Error: The filter file is not a valid PeerGuardian P2B file."), Log::CRITICAL); return ruleCount; } @@ -450,7 +450,7 @@ int FilterParserThread::parseP2BFilterFile() else if (version == 3) { qDebug ("p2b version 3"); unsigned int namecount; - if (!stream.readRawData((char*)&namecount, sizeof(namecount))) { + if (!stream.readRawData(reinterpret_cast(&namecount), sizeof(namecount))) { LogMsg(tr("Parsing Error: The filter file is not a valid PeerGuardian P2B file."), Log::CRITICAL); return ruleCount; } @@ -469,7 +469,7 @@ int FilterParserThread::parseP2BFilterFile() // Reading the ranges unsigned int rangecount; - if (!stream.readRawData((char*)&rangecount, sizeof(rangecount))) { + if (!stream.readRawData(reinterpret_cast(&rangecount), sizeof(rangecount))) { LogMsg(tr("Parsing Error: The filter file is not a valid PeerGuardian P2B file."), Log::CRITICAL); return ruleCount; } @@ -477,9 +477,9 @@ int FilterParserThread::parseP2BFilterFile() rangecount = ntohl(rangecount); unsigned int name, start, end; for (unsigned int i = 0; i < rangecount; ++i) { - if (!stream.readRawData((char*)&name, sizeof(name)) - || !stream.readRawData((char*)&start, sizeof(start)) - || !stream.readRawData((char*)&end, sizeof(end))) { + if (!stream.readRawData(reinterpret_cast(&name), sizeof(name)) + || !stream.readRawData(reinterpret_cast(&start), sizeof(start)) + || !stream.readRawData(reinterpret_cast(&end), sizeof(end))) { LogMsg(tr("Parsing Error: The filter file is not a valid PeerGuardian P2B file."), Log::CRITICAL); return ruleCount; } diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index bb9ec3739..ac899faa6 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -555,7 +555,7 @@ qreal TorrentHandle::progress() const if (m_nativeStatus.total_wanted_done == m_nativeStatus.total_wanted) return 1.; - float progress = (float) m_nativeStatus.total_wanted_done / (float) m_nativeStatus.total_wanted; + float progress = static_cast(m_nativeStatus.total_wanted_done) / m_nativeStatus.total_wanted; Q_ASSERT((progress >= 0.f) && (progress <= 1.f)); return progress; } diff --git a/src/base/filesystemwatcher.cpp b/src/base/filesystemwatcher.cpp index 3eead127d..c4877ee08 100644 --- a/src/base/filesystemwatcher.cpp +++ b/src/base/filesystemwatcher.cpp @@ -224,7 +224,9 @@ bool FileSystemWatcher::isNetworkFileSystem(QString path) // XXX: should we make sure HAVE_STRUCT_FSSTAT_F_FSTYPENAME is defined? return ((strcmp(buf.f_fstypename, "nfs") == 0) || (strcmp(buf.f_fstypename, "cifs") == 0) || (strcmp(buf.f_fstypename, "smbfs") == 0)); #else - return ((buf.f_type == (long)CIFS_MAGIC_NUMBER) || (buf.f_type == (long)NFS_SUPER_MAGIC) || (buf.f_type == (long)SMB_SUPER_MAGIC)); + return ((buf.f_type == static_cast(CIFS_MAGIC_NUMBER)) + || (buf.f_type == static_cast(NFS_SUPER_MAGIC)) + || (buf.f_type == static_cast(SMB_SUPER_MAGIC))); #endif } else { diff --git a/src/base/net/private/geoipdatabase.cpp b/src/base/net/private/geoipdatabase.cpp index 8dec63742..9594ac929 100644 --- a/src/base/net/private/geoipdatabase.cpp +++ b/src/base/net/private/geoipdatabase.cpp @@ -105,7 +105,7 @@ GeoIPDatabase *GeoIPDatabase::load(const QString &filename, QString &error) db = new GeoIPDatabase(file.size()); - if (file.read((char *)db->m_data, db->m_size) != db->m_size) { + if (file.read(reinterpret_cast(db->m_data), db->m_size) != db->m_size) { error = file.errorString(); delete db; return 0; @@ -130,7 +130,7 @@ GeoIPDatabase *GeoIPDatabase::load(const QByteArray &data, QString &error) db = new GeoIPDatabase(data.size()); - memcpy((char *)db->m_data, data.constData(), db->m_size); + memcpy(reinterpret_cast(db->m_data), data.constData(), db->m_size); if (!db->parseMetadata(db->readMetadata(), error) || !db->loadDB(error)) { delete db; diff --git a/src/base/preferences.cpp b/src/base/preferences.cpp index bf376b676..810413620 100644 --- a/src/base/preferences.cpp +++ b/src/base/preferences.cpp @@ -406,12 +406,12 @@ void Preferences::setSchedulerEndTime(const QTime &time) scheduler_days Preferences::getSchedulerDays() const { - return (scheduler_days)value("Preferences/Scheduler/days", EVERY_DAY).toInt(); + return static_cast(value("Preferences/Scheduler/days", EVERY_DAY).toInt()); } void Preferences::setSchedulerDays(scheduler_days days) { - setValue("Preferences/Scheduler/days", (int)days); + setValue("Preferences/Scheduler/days", static_cast(days)); } // Search diff --git a/src/base/utils/random.cpp b/src/base/utils/random.cpp index cb8e3e250..add0b4e16 100644 --- a/src/base/utils/random.cpp +++ b/src/base/utils/random.cpp @@ -56,7 +56,7 @@ uint32_t Utils::Random::rand(const uint32_t min, const uint32_t max) static thread_local std::mt19937 generator{ hasTrueRandomDevice ? std::random_device{}() - : (std::random_device::result_type) std::chrono::system_clock::now().time_since_epoch().count() + : static_cast(std::chrono::system_clock::now().time_since_epoch().count()) }; #endif diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 77574aacb..0b0133202 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1092,7 +1092,7 @@ bool MainWindow::event(QEvent *e) if (isMinimized()) { qDebug("minimisation"); if (m_systrayIcon && Preferences::instance()->minimizeToTray()) { - qDebug("Has active window: %d", (int)(qApp->activeWindow() != 0)); + qDebug() << "Has active window:" << (qApp->activeWindow() != nullptr); // Check if there is a modal window bool hasModalWindow = false; foreach (QWidget *widget, QApplication::allWidgets()) { diff --git a/src/gui/messageboxraised.cpp b/src/gui/messageboxraised.cpp index ac427e77f..902697628 100644 --- a/src/gui/messageboxraised.cpp +++ b/src/gui/messageboxraised.cpp @@ -37,7 +37,7 @@ MessageBoxRaised::MessageBoxRaised(QMessageBox::Icon icon, const QString &title, QMessageBox::StandardButton MessageBoxRaised::impl(const QMessageBox::Icon &icon, QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) { MessageBoxRaised dlg(icon, title, text, buttons, parent); dlg.setDefaultButton(defaultButton); - return (QMessageBox::StandardButton)dlg.exec(); + return static_cast(dlg.exec()); } QMessageBox::StandardButton MessageBoxRaised::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) { diff --git a/src/gui/optionsdlg.cpp b/src/gui/optionsdlg.cpp index 2b3835ad8..3513c954a 100644 --- a/src/gui/optionsdlg.cpp +++ b/src/gui/optionsdlg.cpp @@ -577,7 +577,7 @@ void OptionsDialog::saveOptions() session->setBandwidthSchedulerEnabled(m_ui->check_schedule->isChecked()); pref->setSchedulerStartTime(m_ui->schedule_from->time()); pref->setSchedulerEndTime(m_ui->schedule_to->time()); - pref->setSchedulerDays((scheduler_days)m_ui->schedule_days->currentIndex()); + pref->setSchedulerDays(static_cast(m_ui->schedule_days->currentIndex())); auto proxyConfigManager = Net::ProxyConfigurationManager::instance(); Net::ProxyConfiguration proxyConf; @@ -967,7 +967,7 @@ void OptionsDialog::loadOptions() m_ui->check_schedule->setChecked(session->isBandwidthSchedulerEnabled()); m_ui->schedule_from->setTime(pref->getSchedulerStartTime()); m_ui->schedule_to->setTime(pref->getSchedulerEndTime()); - m_ui->schedule_days->setCurrentIndex((int)pref->getSchedulerDays()); + m_ui->schedule_days->setCurrentIndex(static_cast(pref->getSchedulerDays())); // End Speed preferences // Bittorrent preferences @@ -1024,7 +1024,7 @@ void OptionsDialog::loadOptions() m_ui->checkBypassLocalAuth->setChecked(!pref->isWebUiLocalAuthEnabled()); m_ui->checkDynDNS->setChecked(pref->isDynDNSEnabled()); - m_ui->comboDNSService->setCurrentIndex((int)pref->getDynDNSService()); + m_ui->comboDNSService->setCurrentIndex(static_cast(pref->getDynDNSService())); m_ui->domainNameTxt->setText(pref->getDynDomainName()); m_ui->DNSUsernameTxt->setText(pref->getDynDNSUsername()); m_ui->DNSPasswordTxt->setText(pref->getDynDNSPassword()); diff --git a/src/gui/powermanagement/powermanagement_x11.cpp b/src/gui/powermanagement/powermanagement_x11.cpp index b5bd2196c..14958a62f 100644 --- a/src/gui/powermanagement/powermanagement_x11.cpp +++ b/src/gui/powermanagement/powermanagement_x11.cpp @@ -117,9 +117,9 @@ void PowerManagementInhibitor::RequestBusy() QList args; args << "qBittorrent"; - if (m_use_gsm) args << (uint)0; + if (m_use_gsm) args << 0u; args << "Active torrents are presented"; - if (m_use_gsm) args << (uint)8; + if (m_use_gsm) args << 8u; call.setArguments(args); QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000); diff --git a/src/gui/previewlistdelegate.h b/src/gui/previewlistdelegate.h index 7a42653c3..6dfb085ce 100644 --- a/src/gui/previewlistdelegate.h +++ b/src/gui/previewlistdelegate.h @@ -67,7 +67,7 @@ class PreviewListDelegate: public QItemDelegate { qreal progress = index.data().toDouble()*100.; newopt.rect = opt.rect; newopt.text = ((progress == 100.0) ? QString("100%") : Utils::String::fromDouble(progress, 1) + "%"); - newopt.progress = (int)progress; + newopt.progress = static_cast(progress); newopt.maximum = 100; newopt.minimum = 0; newopt.state |= QStyle::State_Enabled; diff --git a/src/gui/properties/pieceavailabilitybar.cpp b/src/gui/properties/pieceavailabilitybar.cpp index fcbf44bb2..0ce3f2618 100644 --- a/src/gui/properties/pieceavailabilitybar.cpp +++ b/src/gui/properties/pieceavailabilitybar.cpp @@ -44,7 +44,7 @@ QVector PieceAvailabilityBar::intToFloatVector(const QVector &vecin, QVector result(reqSize, 0.0); if (vecin.isEmpty()) return result; - const float ratio = vecin.size() / (float)reqSize; + const float ratio = static_cast(vecin.size()) / reqSize; const int maxElement = *std::max_element(vecin.begin(), vecin.end()); @@ -112,7 +112,7 @@ QVector PieceAvailabilityBar::intToFloatVector(const QVector &vecin, value /= ratio * maxElement; // float precision sometimes gives > 1, because in not possible to store irrational numbers - value = qMin(value, (float)1.0); + value = qMin(value, 1.0f); result[x] = value; } diff --git a/src/gui/statsdialog.cpp b/src/gui/statsdialog.cpp index 50432db33..f73d61164 100644 --- a/src/gui/statsdialog.cpp +++ b/src/gui/statsdialog.cpp @@ -71,7 +71,7 @@ void StatsDialog::update() // Global ratio m_ui->labelGlobalRatio->setText( ((atd > 0) && (atu > 0)) - ? Utils::String::fromDouble((qreal)atu / (qreal)atd, 2) + ? Utils::String::fromDouble(static_cast(atu) / atd, 2) : "-"); // Cache hits qreal readRatio = cs.readRatio; diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index ace9868a8..bae6d58be 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -82,9 +82,9 @@ void TorrentContentModel::updateFilesProgress(const QVector &fp) void TorrentContentModel::updateFilesPriorities(const QVector &fprio) { - Q_ASSERT(m_filesIndex.size() == (int)fprio.size()); + Q_ASSERT(m_filesIndex.size() == fprio.size()); // XXX: Why is this necessary? - if (m_filesIndex.size() != (int)fprio.size()) + if (m_filesIndex.size() != fprio.size()) return; emit layoutAboutToBeChanged(); diff --git a/src/gui/transferlistdelegate.cpp b/src/gui/transferlistdelegate.cpp index eb1f8adde..6187cc245 100644 --- a/src/gui/transferlistdelegate.cpp +++ b/src/gui/transferlistdelegate.cpp @@ -162,7 +162,7 @@ void TransferListDelegate::paint(QPainter * painter, const QStyleOptionViewItem qreal progress = index.data().toDouble() * 100.; newopt.rect = opt.rect; newopt.text = ((progress == 100.0) ? QString("100%") : Utils::String::fromDouble(progress, 1) + "%"); - newopt.progress = (int)progress; + newopt.progress = static_cast(progress); newopt.maximum = 100; newopt.minimum = 0; newopt.state |= QStyle::State_Enabled; diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index e2e438466..8f77bef06 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -449,7 +449,7 @@ void TransferListWidget::setDlLimitSelectedTorrents() if (!ok) return; foreach (BitTorrent::TorrentHandle *const torrent, TorrentsList) { - qDebug("Applying download speed limit of %ld Kb/s to torrent %s", (long) (newLimit / 1024.), qPrintable(torrent->hash())); + qDebug("Applying download speed limit of %ld Kb/s to torrent %s", (newLimit / 1024l), qPrintable(torrent->hash())); torrent->setDownloadLimit(newLimit); } } @@ -474,7 +474,7 @@ void TransferListWidget::setUpLimitSelectedTorrents() if (!ok) return; foreach (BitTorrent::TorrentHandle *const torrent, TorrentsList) { - qDebug("Applying upload speed limit of %ld Kb/s to torrent %s", (long) (newLimit / 1024.), qPrintable(torrent->hash())); + qDebug("Applying upload speed limit of %ld Kb/s to torrent %s", (newLimit / 1024l), qPrintable(torrent->hash())); torrent->setUploadLimit(newLimit); } } diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 869221cf7..4eef96403 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -373,7 +373,7 @@ QByteArray btjson::getSyncMainData(int acceptedResponseId, QVariantMap &lastData if (lastData.contains("torrents") && lastData["torrents"].toHash().contains(torrent->hash()) && lastData["torrents"].toHash()[torrent->hash()].toMap().contains(KEY_TORRENT_LAST_ACTIVITY_TIME)) { uint lastValue = lastData["torrents"].toHash()[torrent->hash()].toMap()[KEY_TORRENT_LAST_ACTIVITY_TIME].toUInt(); - if (qAbs((int)(lastValue - map[KEY_TORRENT_LAST_ACTIVITY_TIME].toUInt())) < 15) + if (qAbs(static_cast(lastValue - map[KEY_TORRENT_LAST_ACTIVITY_TIME].toUInt())) < 15) map[KEY_TORRENT_LAST_ACTIVITY_TIME] = lastValue; } @@ -595,8 +595,8 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash) dataDict[KEY_PROP_CREATED_BY] = torrent->creator(); dataDict[KEY_PROP_ADDITION_DATE] = torrent->addedTime().toTime_t(); if (torrent->hasMetadata()) { - dataDict[KEY_PROP_LAST_SEEN] = torrent->lastSeenComplete().isValid() ? (int)torrent->lastSeenComplete().toTime_t() : -1; - dataDict[KEY_PROP_COMPLETION_DATE] = torrent->completedTime().isValid() ? (int)torrent->completedTime().toTime_t() : -1; + dataDict[KEY_PROP_LAST_SEEN] = torrent->lastSeenComplete().isValid() ? static_cast(torrent->lastSeenComplete().toTime_t()) : -1; + dataDict[KEY_PROP_COMPLETION_DATE] = torrent->completedTime().isValid() ? static_cast(torrent->completedTime().toTime_t()) : -1; dataDict[KEY_PROP_CREATION_DATE] = torrent->creationDate().toTime_t(); } else { @@ -743,7 +743,7 @@ QVariantMap getTranserInfoMap() map[KEY_TRANSFER_ALLTIME_DL] = atd; map[KEY_TRANSFER_ALLTIME_UL] = atu; map[KEY_TRANSFER_TOTAL_WASTE_SESSION] = sessionStatus.totalWasted; - map[KEY_TRANSFER_GLOBAL_RATIO] = ( atd > 0 && atu > 0 ) ? Utils::String::fromDouble((qreal)atu / (qreal)atd, 2) : "-"; + map[KEY_TRANSFER_GLOBAL_RATIO] = ( atd > 0 && atu > 0 ) ? Utils::String::fromDouble(static_cast(atu) / atd, 2) : "-"; map[KEY_TRANSFER_TOTAL_PEER_CONNECTIONS] = sessionStatus.peersCount; qreal readRatio = cacheStatus.readRatio;