From 896a66e6d815e9122d5f26831a193226076feb9e Mon Sep 17 00:00:00 2001 From: Gianluca Recchia Date: Sun, 28 Oct 2018 15:47:24 +0100 Subject: [PATCH] Improve readability and type-safety Use nullptr instead of 0 or NULL to initialize a null pointer. In some cases, readability was enhanced by replacing 0 with more meaningful values according to the type of the pointer being initialized. --- src/autotype/AutoType.cpp | 2 +- src/autotype/ShortcutWidget.cpp | 4 ++-- src/autotype/xcb/AutoTypeXCB.cpp | 6 +++--- src/browser/BrowserEntryConfig.h | 2 +- src/browser/BrowserService.cpp | 6 +++--- src/browser/HostInstaller.cpp | 2 +- src/browser/NativeMessagingHost.h | 2 +- src/core/PasswordGenerator.cpp | 6 +++--- src/core/ScreenLockListenerPrivate.h | 4 ++-- src/core/ScreenLockListenerWin.h | 2 +- src/crypto/Crypto.cpp | 2 +- src/gui/CategoryListWidget.h | 4 ++-- src/gui/DatabaseOpenWidget.cpp | 2 +- src/gui/DatabaseTabWidget.cpp | 4 ++-- src/gui/KMessageWidget.cpp | 4 ++-- src/gui/MessageWidget.h | 2 +- src/gui/SearchWidget.h | 4 ++-- src/gui/entry/EditEntryWidget.cpp | 12 ++++++------ src/gui/entry/EditEntryWidget_p.h | 2 +- src/gui/entry/EntryModel.cpp | 2 +- src/keys/drivers/YubiKey.cpp | 16 ++++++++-------- src/streams/qtiocompressor.cpp | 4 ++-- 22 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/autotype/AutoType.cpp b/src/autotype/AutoType.cpp index eee93c52..9d6b7555 100644 --- a/src/autotype/AutoType.cpp +++ b/src/autotype/AutoType.cpp @@ -43,7 +43,7 @@ AutoType::AutoType(QObject* parent, bool test) : QObject(parent) , m_autoTypeDelay(0) , m_currentGlobalKey(static_cast(0)) - , m_currentGlobalModifiers(0) + , m_currentGlobalModifiers(nullptr) , m_pluginLoader(new QPluginLoader(this)) , m_plugin(nullptr) , m_executor(nullptr) diff --git a/src/autotype/ShortcutWidget.cpp b/src/autotype/ShortcutWidget.cpp index 95174e43..3dcc669d 100644 --- a/src/autotype/ShortcutWidget.cpp +++ b/src/autotype/ShortcutWidget.cpp @@ -24,7 +24,7 @@ ShortcutWidget::ShortcutWidget(QWidget* parent) : QLineEdit(parent) , m_key(static_cast(0)) - , m_modifiers(0) + , m_modifiers(nullptr) , m_locked(false) { setReadOnly(true); @@ -58,7 +58,7 @@ void ShortcutWidget::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers) void ShortcutWidget::resetShortcut() { m_key = static_cast(0); - m_modifiers = 0; + m_modifiers = nullptr; m_locked = false; autoType()->unregisterGlobalShortcut(); } diff --git a/src/autotype/xcb/AutoTypeXCB.cpp b/src/autotype/xcb/AutoTypeXCB.cpp index d2030b77..5198468c 100644 --- a/src/autotype/xcb/AutoTypeXCB.cpp +++ b/src/autotype/xcb/AutoTypeXCB.cpp @@ -50,7 +50,7 @@ AutoTypePlatformX11::AutoTypePlatformX11() << "xfce4-panel"; // Xfce 4 m_currentGlobalKey = static_cast(0); - m_currentGlobalModifiers = 0; + m_currentGlobalModifiers = nullptr; m_keysymTable = nullptr; m_xkb = nullptr; @@ -197,7 +197,7 @@ void AutoTypePlatformX11::unregisterGlobalShortcut(Qt::Key key, Qt::KeyboardModi XUngrabKey(m_dpy, keycode, nativeModifiers | Mod2Mask | LockMask, m_rootWindow); m_currentGlobalKey = static_cast(0); - m_currentGlobalModifiers = 0; + m_currentGlobalModifiers = nullptr; m_currentGlobalKeycode = 0; m_currentGlobalNativeModifiers = 0; } @@ -496,7 +496,7 @@ void AutoTypePlatformX11::updateKeymap() m_xkb = getKeyboard(); XDisplayKeycodes(m_dpy, &m_minKeycode, &m_maxKeycode); - if (m_keysymTable != NULL) + if (m_keysymTable != nullptr) XFree(m_keysymTable); m_keysymTable = XGetKeyboardMapping(m_dpy, m_minKeycode, m_maxKeycode - m_minKeycode + 1, &m_keysymPerKeycode); diff --git a/src/browser/BrowserEntryConfig.h b/src/browser/BrowserEntryConfig.h index 3b808b7e..f1363f42 100644 --- a/src/browser/BrowserEntryConfig.h +++ b/src/browser/BrowserEntryConfig.h @@ -35,7 +35,7 @@ class BrowserEntryConfig : public QObject Q_PROPERTY(QString Realm READ realm WRITE setRealm) public: - BrowserEntryConfig(QObject* object = 0); + BrowserEntryConfig(QObject* object = nullptr); bool load(const Entry* entry); void save(Entry* entry); diff --git a/src/browser/BrowserService.cpp b/src/browser/BrowserService.cpp index 824cc94b..8128ee14 100644 --- a/src/browser/BrowserService.cpp +++ b/src/browser/BrowserService.cpp @@ -481,16 +481,16 @@ void BrowserService::convertAttributesToCustomData(Database *currentDb) progress.reset(); if (counter > 0) { - QMessageBox::information(0, tr("KeePassXC: Converted KeePassHTTP attributes"), + QMessageBox::information(nullptr, tr("KeePassXC: Converted KeePassHTTP attributes"), tr("Successfully converted attributes from %1 entry(s).\n" "Moved %2 keys to custom data.", "").arg(counter).arg(keyCounter), QMessageBox::Ok); } else if (counter == 0 && keyCounter > 0) { - QMessageBox::information(0, tr("KeePassXC: Converted KeePassHTTP attributes"), + QMessageBox::information(nullptr, tr("KeePassXC: Converted KeePassHTTP attributes"), tr("Successfully moved %n keys to custom data.", "", keyCounter), QMessageBox::Ok); } else { - QMessageBox::information(0, tr("KeePassXC: No entry with KeePassHTTP attributes found!"), + QMessageBox::information(nullptr, tr("KeePassXC: No entry with KeePassHTTP attributes found!"), tr("The active database does not contain an entry with KeePassHTTP attributes."), QMessageBox::Ok); } diff --git a/src/browser/HostInstaller.cpp b/src/browser/HostInstaller.cpp index a3ad608d..f4585feb 100644 --- a/src/browser/HostInstaller.cpp +++ b/src/browser/HostInstaller.cpp @@ -111,7 +111,7 @@ void HostInstaller::installBrowser(SupportedBrowsers browser, // Always create the script file QJsonObject script = constructFile(browser, proxy, location); if (!saveFile(browser, script)) { - QMessageBox::critical(0, + QMessageBox::critical(nullptr, tr("KeePassXC: Cannot save file!"), tr("Cannot save the native messaging script file."), QMessageBox::Ok); diff --git a/src/browser/NativeMessagingHost.h b/src/browser/NativeMessagingHost.h index 869af9e2..da9ac534 100644 --- a/src/browser/NativeMessagingHost.h +++ b/src/browser/NativeMessagingHost.h @@ -31,7 +31,7 @@ class NativeMessagingHost : public NativeMessagingBase typedef QList SocketList; public: - explicit NativeMessagingHost(DatabaseTabWidget* parent = 0, const bool enabled = false); + explicit NativeMessagingHost(DatabaseTabWidget* parent = nullptr, const bool enabled = false); ~NativeMessagingHost(); int init(); void run(); diff --git a/src/core/PasswordGenerator.cpp b/src/core/PasswordGenerator.cpp index 124b9989..69a0dfb3 100644 --- a/src/core/PasswordGenerator.cpp +++ b/src/core/PasswordGenerator.cpp @@ -25,15 +25,15 @@ const char* PasswordGenerator::DefaultExcludedChars = ""; PasswordGenerator::PasswordGenerator() : m_length(0) - , m_classes(0) - , m_flags(0) + , m_classes(nullptr) + , m_flags(nullptr) , m_excluded(PasswordGenerator::DefaultExcludedChars) { } double PasswordGenerator::calculateEntropy(const QString& password) { - return ZxcvbnMatch(password.toLatin1(), 0, 0); + return ZxcvbnMatch(password.toLatin1(), nullptr, nullptr); } void PasswordGenerator::setLength(int length) diff --git a/src/core/ScreenLockListenerPrivate.h b/src/core/ScreenLockListenerPrivate.h index 8ecad17d..a7c08068 100644 --- a/src/core/ScreenLockListenerPrivate.h +++ b/src/core/ScreenLockListenerPrivate.h @@ -24,10 +24,10 @@ class ScreenLockListenerPrivate : public QObject { Q_OBJECT public: - static ScreenLockListenerPrivate* instance(QWidget* parent = 0); + static ScreenLockListenerPrivate* instance(QWidget* parent = nullptr); protected: - ScreenLockListenerPrivate(QWidget* parent = 0); + ScreenLockListenerPrivate(QWidget* parent = nullptr); signals: void screenLocked(); diff --git a/src/core/ScreenLockListenerWin.h b/src/core/ScreenLockListenerWin.h index 0778c99d..523ae5d0 100644 --- a/src/core/ScreenLockListenerWin.h +++ b/src/core/ScreenLockListenerWin.h @@ -27,7 +27,7 @@ class ScreenLockListenerWin : public ScreenLockListenerPrivate, public QAbstract { Q_OBJECT public: - explicit ScreenLockListenerWin(QWidget* parent = 0); + explicit ScreenLockListenerWin(QWidget* parent = nullptr); ~ScreenLockListenerWin(); virtual bool nativeEventFilter(const QByteArray& eventType, void* message, long*) override; diff --git a/src/crypto/Crypto.cpp b/src/crypto/Crypto.cpp index 0ed6f003..fffcddfb 100644 --- a/src/crypto/Crypto.cpp +++ b/src/crypto/Crypto.cpp @@ -336,4 +336,4 @@ bool Crypto::testChaCha20() } return true; -} \ No newline at end of file +} diff --git a/src/gui/CategoryListWidget.h b/src/gui/CategoryListWidget.h index 3f08fe38..7873a3d3 100644 --- a/src/gui/CategoryListWidget.h +++ b/src/gui/CategoryListWidget.h @@ -35,7 +35,7 @@ class CategoryListWidget : public QWidget Q_OBJECT public: - CategoryListWidget(QWidget* parent = 0); + CategoryListWidget(QWidget* parent = nullptr); ~CategoryListWidget(); int currentCategory(); @@ -90,4 +90,4 @@ private: Q_DISABLE_COPY(CategoryListWidgetDelegate) }; -#endif \ No newline at end of file +#endif diff --git a/src/gui/DatabaseOpenWidget.cpp b/src/gui/DatabaseOpenWidget.cpp index 892c41d3..4d906db3 100644 --- a/src/gui/DatabaseOpenWidget.cpp +++ b/src/gui/DatabaseOpenWidget.cpp @@ -122,7 +122,7 @@ void DatabaseOpenWidget::hideEvent(QHideEvent* event) #ifdef WITH_XC_YUBIKEY // Don't listen to any Yubikey events if we are hidden - disconnect(YubiKey::instance(), 0, this, 0); + disconnect(YubiKey::instance(), nullptr, this, nullptr); m_yubiKeyBeingPolled = false; #endif } diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index dd36ddae..b502c116 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -419,7 +419,7 @@ bool DatabaseTabWidget::saveDatabaseAs(Database* db) oldFilePath, tr("KeePass 2 Database").append(" (*.kdbx)"), nullptr, - 0, + nullptr, "kdbx"); if (!newFilePath.isEmpty()) { // Ensure we don't recurse back into this function @@ -488,7 +488,7 @@ void DatabaseTabWidget::exportToCsv() } QString fileName = fileDialog()->getSaveFileName( - this, tr("Export database to CSV file"), QString(), tr("CSV file").append(" (*.csv)"), nullptr, 0, "csv"); + this, tr("Export database to CSV file"), QString(), tr("CSV file").append(" (*.csv)"), nullptr, nullptr, "csv"); if (fileName.isEmpty()) { return; } diff --git a/src/gui/KMessageWidget.cpp b/src/gui/KMessageWidget.cpp index 2e68975e..3de1a15b 100644 --- a/src/gui/KMessageWidget.cpp +++ b/src/gui/KMessageWidget.cpp @@ -411,7 +411,7 @@ void KMessageWidget::removeAction(QAction *action) void KMessageWidget::animatedShow() { - if (!style()->styleHint(QStyle::SH_Widget_Animate, 0, this)) { + if (!style()->styleHint(QStyle::SH_Widget_Animate, nullptr, this)) { show(); emit showAnimationFinished(); return; @@ -436,7 +436,7 @@ void KMessageWidget::animatedShow() void KMessageWidget::animatedHide() { - if (!style()->styleHint(QStyle::SH_Widget_Animate, 0, this)) { + if (!style()->styleHint(QStyle::SH_Widget_Animate, nullptr, this)) { hide(); emit hideAnimationFinished(); return; diff --git a/src/gui/MessageWidget.h b/src/gui/MessageWidget.h index 73f0b210..eac50601 100644 --- a/src/gui/MessageWidget.h +++ b/src/gui/MessageWidget.h @@ -28,7 +28,7 @@ class MessageWidget : public KMessageWidget Q_OBJECT public: - explicit MessageWidget(QWidget* parent = 0); + explicit MessageWidget(QWidget* parent = nullptr); int autoHideTimeout() const; diff --git a/src/gui/SearchWidget.h b/src/gui/SearchWidget.h index 1d95c664..0ec3287c 100644 --- a/src/gui/SearchWidget.h +++ b/src/gui/SearchWidget.h @@ -35,7 +35,7 @@ class SearchWidget : public QWidget Q_OBJECT public: - explicit SearchWidget(QWidget* parent = 0); + explicit SearchWidget(QWidget* parent = nullptr); ~SearchWidget(); void connectSignals(SignalMultiplexer& mx); @@ -55,7 +55,7 @@ signals: void enterPressed(); public slots: - void databaseChanged(DatabaseWidget* dbWidget = 0); + void databaseChanged(DatabaseWidget* dbWidget = nullptr); private slots: void startSearchTimer(); diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 26bb419c..49c19280 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -1269,13 +1269,13 @@ QMenu* EditEntryWidget::createPresetsMenu() QMenu* expirePresetsMenu = new QMenu(this); expirePresetsMenu->addAction(tr("Tomorrow"))->setData(QVariant::fromValue(TimeDelta::fromDays(1))); expirePresetsMenu->addSeparator(); - expirePresetsMenu->addAction(tr("%n week(s)", 0, 1))->setData(QVariant::fromValue(TimeDelta::fromDays(7))); - expirePresetsMenu->addAction(tr("%n week(s)", 0, 2))->setData(QVariant::fromValue(TimeDelta::fromDays(14))); - expirePresetsMenu->addAction(tr("%n week(s)", 0, 3))->setData(QVariant::fromValue(TimeDelta::fromDays(21))); + expirePresetsMenu->addAction(tr("%n week(s)", nullptr, 1))->setData(QVariant::fromValue(TimeDelta::fromDays(7))); + expirePresetsMenu->addAction(tr("%n week(s)", nullptr, 2))->setData(QVariant::fromValue(TimeDelta::fromDays(14))); + expirePresetsMenu->addAction(tr("%n week(s)", nullptr, 3))->setData(QVariant::fromValue(TimeDelta::fromDays(21))); expirePresetsMenu->addSeparator(); - expirePresetsMenu->addAction(tr("%n month(s)", 0, 1))->setData(QVariant::fromValue(TimeDelta::fromMonths(1))); - expirePresetsMenu->addAction(tr("%n month(s)", 0, 3))->setData(QVariant::fromValue(TimeDelta::fromMonths(3))); - expirePresetsMenu->addAction(tr("%n month(s)", 0, 6))->setData(QVariant::fromValue(TimeDelta::fromMonths(6))); + expirePresetsMenu->addAction(tr("%n month(s)", nullptr, 1))->setData(QVariant::fromValue(TimeDelta::fromMonths(1))); + expirePresetsMenu->addAction(tr("%n month(s)", nullptr, 3))->setData(QVariant::fromValue(TimeDelta::fromMonths(3))); + expirePresetsMenu->addAction(tr("%n month(s)", nullptr, 6))->setData(QVariant::fromValue(TimeDelta::fromMonths(6))); expirePresetsMenu->addSeparator(); expirePresetsMenu->addAction(tr("%n year(s)", 0, 1))->setData(QVariant::fromValue(TimeDelta::fromYears(1))); expirePresetsMenu->addAction(tr("%n year(s)", 0, 2))->setData(QVariant::fromValue(TimeDelta::fromYears(2))); diff --git a/src/gui/entry/EditEntryWidget_p.h b/src/gui/entry/EditEntryWidget_p.h index 0ba01cd5..dd0bac8b 100644 --- a/src/gui/entry/EditEntryWidget_p.h +++ b/src/gui/entry/EditEntryWidget_p.h @@ -24,7 +24,7 @@ class AttributesListView : public QListView { public: - explicit AttributesListView(QWidget* parent = 0) + explicit AttributesListView(QWidget* parent = nullptr) : QListView(parent) { setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); diff --git a/src/gui/entry/EntryModel.cpp b/src/gui/entry/EntryModel.cpp index fa3db177..8cbf4bfe 100644 --- a/src/gui/entry/EntryModel.cpp +++ b/src/gui/entry/EntryModel.cpp @@ -331,7 +331,7 @@ QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int ro Qt::DropActions EntryModel::supportedDropActions() const { - return 0; + return Qt::IgnoreAction; } Qt::DropActions EntryModel::supportedDragActions() const diff --git a/src/keys/drivers/YubiKey.cpp b/src/keys/drivers/YubiKey.cpp index b9491619..b6905fc3 100644 --- a/src/keys/drivers/YubiKey.cpp +++ b/src/keys/drivers/YubiKey.cpp @@ -37,8 +37,8 @@ #define m_ykds (static_cast(m_ykds_void)) YubiKey::YubiKey() - : m_yk_void(NULL) - , m_ykds_void(NULL) + : m_yk_void(nullptr) + , m_ykds_void(nullptr) , m_mutex(QMutex::Recursive) { } @@ -59,7 +59,7 @@ bool YubiKey::init() m_mutex.lock(); // previously initialized - if (m_yk != NULL && m_ykds != NULL) { + if (m_yk != nullptr && m_ykds != nullptr) { if (yk_get_status(m_yk, m_ykds)) { // Still connected @@ -78,15 +78,15 @@ bool YubiKey::init() // TODO: handle multiple attached hardware devices m_yk_void = static_cast(yk_open_first_key()); - if (m_yk == NULL) { + if (m_yk == nullptr) { m_mutex.unlock(); return false; } m_ykds_void = static_cast(ykds_alloc()); - if (m_ykds == NULL) { + if (m_ykds == nullptr) { yk_close_key(m_yk); - m_yk_void = NULL; + m_yk_void = nullptr; m_mutex.unlock(); return false; } @@ -101,12 +101,12 @@ bool YubiKey::deinit() if (m_yk) { yk_close_key(m_yk); - m_yk_void = NULL; + m_yk_void = nullptr; } if (m_ykds) { ykds_free(m_ykds); - m_ykds_void = NULL; + m_ykds_void = nullptr; } m_mutex.unlock(); diff --git a/src/streams/qtiocompressor.cpp b/src/streams/qtiocompressor.cpp index 97955e47..6e3c69ff 100644 --- a/src/streams/qtiocompressor.cpp +++ b/src/streams/qtiocompressor.cpp @@ -116,7 +116,7 @@ QtIOCompressorPrivate::~QtIOCompressorPrivate() void QtIOCompressorPrivate::flushZlib(int flushMode) { // No input. - zlibStream.next_in = 0; + zlibStream.next_in = nullptr; zlibStream.avail_in = 0; int status; do { @@ -387,7 +387,7 @@ bool QtIOCompressor::open(OpenMode mode) if (read) { d->state = QtIOCompressorPrivate::NotReadFirstByte; d->zlibStream.avail_in = 0; - d->zlibStream.next_in = 0; + d->zlibStream.next_in = nullptr; if (d->streamFormat == QtIOCompressor::ZlibFormat) { status = inflateInit(&d->zlibStream); } else {