From b29a9ce142be1834a5af6d2b652c038e838c515b Mon Sep 17 00:00:00 2001 From: ozone10 Date: Mon, 19 Sep 2022 08:51:31 +0200 Subject: [PATCH] Code enhancement - removing gcc warning Fix conversion-null, nonnull-compare, dangling-else, address warnings, add initializers. Fix #12198, close #12199 --- PowerEditor/src/MISC/Common/Common.cpp | 15 +++++++-------- PowerEditor/src/Notepad_plus.cpp | 14 ++++++-------- PowerEditor/src/NppBigSwitch.cpp | 16 +++++++--------- PowerEditor/src/NppIO.cpp | 18 +++++++++--------- PowerEditor/src/ScintillaComponent/Buffer.h | 12 ++++++------ .../ScintillaComponent/SmartHighlighter.cpp | 8 ++++---- .../ScintillaComponent/UserDefineDialog.cpp | 10 +++++----- 7 files changed, 44 insertions(+), 49 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index b08475f63..52fc13f89 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -132,7 +132,7 @@ void writeLog(const TCHAR *logFileName, const char *log2write) if (hFile != INVALID_HANDLE_VALUE) { - LARGE_INTEGER offset; + LARGE_INTEGER offset{}; offset.QuadPart = 0; ::SetFilePointerEx(hFile, offset, NULL, FILE_END); @@ -189,7 +189,7 @@ generic_string getFolderName(HWND parent, const TCHAR *defaultDir) void ClientRectToScreenRect(HWND hWnd, RECT* rect) { - POINT pt; + POINT pt{}; pt.x = rect->left; pt.y = rect->top; @@ -230,7 +230,7 @@ std::vector tokenizeString(const generic_string & tokenString, c void ScreenRectToClientRect(HWND hWnd, RECT* rect) { - POINT pt; + POINT pt{}; pt.x = rect->left; pt.y = rect->top; @@ -262,7 +262,7 @@ bool isInList(const TCHAR *token, const TCHAR *list) const size_t wordLen = 64; size_t listLen = lstrlen(list); - TCHAR word[wordLen]; + TCHAR word[wordLen] = { '\0' }; size_t i = 0; size_t j = 0; @@ -1149,7 +1149,7 @@ bool isCertificateValidated(const generic_string & fullFilePath, const generic_s DWORD dwFormatType = 0; PCMSG_SIGNER_INFO pSignerInfo = NULL; DWORD dwSignerInfo = 0; - CERT_INFO CertInfo; + CERT_INFO CertInfo{}; LPTSTR szName = NULL; generic_string subjectName; @@ -1285,11 +1285,10 @@ bool isAssoCommandExisting(LPCTSTR FullPathName) // check if association exist hres = AssocQueryString(ASSOCF_VERIFY|ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_COMMAND, ext, NULL, buffer, &bufferLen); - + isAssoCommandExisting = (hres == S_OK) // check if association exist and no error - && (buffer != NULL) // check if buffer is not NULL && (wcsstr(buffer, TEXT("notepad++.exe")) == NULL); // check association with notepad++ - + } return isAssoCommandExisting; } diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index f1f9757e6..706973a24 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -491,7 +491,7 @@ LRESULT Notepad_plus::init(HWND hwnd) int numLangs = ::GetMenuItemCount(hLangMenu); const int bufferSize = 100; - TCHAR buffer[bufferSize]; + TCHAR buffer[bufferSize] = { '\0' }; const TCHAR* lexerNameW = wmc.char2wchar(externalLangContainer._name.c_str(), CP_ACP); int x = 0; @@ -609,7 +609,7 @@ LRESULT Notepad_plus::init(HWND hwnd) willBeShown = nppGUI._toolbarShow; // To notify plugins that toolbar icons can be registered - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_TBMODIFICATION; scnN.nmhdr.hwndFrom = hwnd; scnN.nmhdr.idFrom = 0; @@ -820,7 +820,7 @@ bool Notepad_plus::saveGUIParams() // When window is maximized GetWindowPlacement returns window's last non maximized coordinates. // Save them so that those will be used when window is restored next time. - WINDOWPLACEMENT posInfo; + WINDOWPLACEMENT posInfo{}; posInfo.length = sizeof(WINDOWPLACEMENT); ::GetWindowPlacement(_pPublicInterface->getHSelf(), &posInfo); @@ -2948,11 +2948,13 @@ bool removeUnwantedTrailingCharFromUrl (TCHAR const *text, int* length) { if (text [j] == closingParenthesis [i]) count++; - if (text [j] == openingParenthesis [i]) + if (text[j] == openingParenthesis[i]) + { if (count > 0) count--; else return false; + } } if (count != 0) return false; @@ -5937,10 +5939,6 @@ void Notepad_plus::prepareBufferChangedDialog(Buffer * buffer) void Notepad_plus::notifyBufferChanged(Buffer * buffer, int mask) { - // To avoid to crash while MS-DOS style is set as default language, - // Checking the validity of current instance is necessary. - if (!this) return; - NppParameters& nppParam = NppParameters::getInstance(); const NppGUI & nppGUI = nppParam.getNppGUI(); diff --git a/PowerEditor/src/NppBigSwitch.cpp b/PowerEditor/src/NppBigSwitch.cpp index 164f82314..8d7ba9f8f 100644 --- a/PowerEditor/src/NppBigSwitch.cpp +++ b/PowerEditor/src/NppBigSwitch.cpp @@ -123,11 +123,9 @@ LRESULT Notepad_plus_Window::runProc(HWND hwnd, UINT message, WPARAM wParam, LPA } default: { - if (this) - return _notepad_plus_plus_core.process(hwnd, message, wParam, lParam); + return _notepad_plus_plus_core.process(hwnd, message, wParam, lParam); } } - return FALSE; } // Used by NPPM_GETFILENAMEATCURSOR @@ -572,7 +570,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa BufferID id = _pEditView->getCurrentBufferID(); // Notify plugins that current file is about to be closed - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_DOCORDERCHANGED; scnN.nmhdr.hwndFrom = reinterpret_cast(lParam); scnN.nmhdr.idFrom = reinterpret_cast(id); @@ -678,7 +676,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa generic_string pluginMessage { nppParam.getCmdLineParams()._pluginMessage }; if (!pluginMessage.empty()) { - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_CMDLINEPLUGINMSG; scnN.nmhdr.hwndFrom = hwnd; scnN.nmhdr.idFrom = reinterpret_cast(pluginMessage.c_str()); @@ -820,7 +818,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa case NPPM_INTERNAL_PLUGINSHORTCUTMOTIFIED: { - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_SHORTCUTREMAPPED; scnN.nmhdr.hwndFrom = reinterpret_cast(lParam); // ShortcutKey structure scnN.nmhdr.idFrom = (uptr_t)wParam; // cmdID @@ -1269,7 +1267,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa if (message == NPPM_TRIGGERTABBARCONTEXTMENU) { // open here tab menu - NMHDR nmhdr; + NMHDR nmhdr{}; nmhdr.code = NM_RCLICK; nmhdr.hwndFrom = (whichView == MAIN_VIEW)?_mainDocTab.getHSelf():_subDocTab.getHSelf(); @@ -2046,7 +2044,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa _pDocMap->setSyntaxHiliting(); // Notify plugins of update to styles xml - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_WORDSTYLESUPDATED; scnN.nmhdr.hwndFrom = hwnd; scnN.nmhdr.idFrom = (uptr_t) _pEditView->getCurrentBufferID(); @@ -2084,7 +2082,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa } else { - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_BEFORESHUTDOWN; scnN.nmhdr.hwndFrom = hwnd; scnN.nmhdr.idFrom = 0; diff --git a/PowerEditor/src/NppIO.cpp b/PowerEditor/src/NppIO.cpp index 1e4bb20c2..ddb359ff8 100644 --- a/PowerEditor/src/NppIO.cpp +++ b/PowerEditor/src/NppIO.cpp @@ -320,7 +320,7 @@ BufferID Notepad_plus::doOpen(const generic_string& fileName, bool isRecursive, // Notify plugins that current file is about to load // Plugins can should use this notification to filter SCN_MODIFIED - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_FILEBEFORELOAD; scnN.nmhdr.hwndFrom = _pPublicInterface->getHSelf(); scnN.nmhdr.idFrom = 0; @@ -334,7 +334,7 @@ BufferID Notepad_plus::doOpen(const generic_string& fileName, bool isRecursive, BufferID buffer; if (isSnapshotMode) { - buffer = MainFileManager.loadFile(longFileName, NULL, encoding, backupFileName, fileNameTimestamp); + buffer = MainFileManager.loadFile(longFileName, static_cast(NULL), encoding, backupFileName, fileNameTimestamp); if (buffer != BUFFER_INVALID) { @@ -342,7 +342,7 @@ BufferID Notepad_plus::doOpen(const generic_string& fileName, bool isRecursive, if (isSnapshotMode) { // To notify plugins that a snapshot dirty file is loaded on startup - SCNotification scnN2; + SCNotification scnN2{}; scnN2.nmhdr.hwndFrom = 0; scnN2.nmhdr.idFrom = (uptr_t)buffer; scnN2.nmhdr.code = NPPN_SNAPSHOTDIRTYFILELOADED; @@ -354,7 +354,7 @@ BufferID Notepad_plus::doOpen(const generic_string& fileName, bool isRecursive, } else { - buffer = MainFileManager.loadFile(longFileName, NULL, encoding); + buffer = MainFileManager.loadFile(longFileName, static_cast(NULL), encoding); } if (buffer != BUFFER_INVALID) @@ -540,7 +540,7 @@ bool Notepad_plus::doSave(BufferID id, const TCHAR * filename, bool isCopy) return false; } - SCNotification scnN; + SCNotification scnN{}; // Notify plugins that current file is about to be saved if (!isCopy) { @@ -685,7 +685,7 @@ void Notepad_plus::doClose(BufferID id, int whichOne, bool doDeleteBackup) Buffer * buf = MainFileManager.getBufferByID(id); // Notify plugins that current file is about to be closed - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_FILEBEFORECLOSE; scnN.nmhdr.hwndFrom = _pPublicInterface->getHSelf(); scnN.nmhdr.idFrom = (uptr_t)id; @@ -779,7 +779,7 @@ void Notepad_plus::doClose(BufferID id, int whichOne, bool doDeleteBackup) generic_string Notepad_plus::exts2Filters(const generic_string& exts, int maxExtsLen) const { const TCHAR *extStr = exts.c_str(); - TCHAR aExt[MAX_PATH]; + TCHAR aExt[MAX_PATH] = { '\0' }; generic_string filters(TEXT("")); int j = 0; @@ -1783,7 +1783,7 @@ bool Notepad_plus::fileRename(BufferID id) bufferID = _pEditView->getCurrentBufferID(); Buffer * buf = MainFileManager.getBufferByID(bufferID); - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_FILEBEFORERENAME; scnN.nmhdr.hwndFrom = _pPublicInterface->getHSelf(); scnN.nmhdr.idFrom = (uptr_t)bufferID; @@ -1890,7 +1890,7 @@ bool Notepad_plus::fileDelete(BufferID id) if (goAhead) { - SCNotification scnN; + SCNotification scnN{}; scnN.nmhdr.code = NPPN_FILEBEFOREDELETE; scnN.nmhdr.hwndFrom = _pPublicInterface->getHSelf(); scnN.nmhdr.idFrom = (uptr_t)bufferID; diff --git a/PowerEditor/src/ScintillaComponent/Buffer.h b/PowerEditor/src/ScintillaComponent/Buffer.h index 7bcf4f5e4..4f065b58a 100644 --- a/PowerEditor/src/ScintillaComponent/Buffer.h +++ b/PowerEditor/src/ScintillaComponent/Buffer.h @@ -78,7 +78,7 @@ public: void addBufferReference(BufferID id, ScintillaEditView * identifer); //called by Scintilla etc indirectly - BufferID loadFile(const TCHAR * filename, Document doc = NULL, int encoding = -1, const TCHAR *backupFileName = NULL, FILETIME fileNameTimestamp = {}); //ID == BUFFER_INVALID on failure. If Doc == NULL, a new file is created, otherwise data is loaded in given document + BufferID loadFile(const TCHAR * filename, Document doc = static_cast(NULL), int encoding = -1, const TCHAR *backupFileName = nullptr, FILETIME fileNameTimestamp = {}); //ID == BUFFER_INVALID on failure. If Doc == NULL, a new file is created, otherwise data is loaded in given document BufferID newEmptyDocument(); //create Buffer from existing Scintilla, used from new Scintillas. If dontIncrease = true, then the new document number isnt increased afterwards. //usefull for temporary but neccesary docs @@ -174,9 +174,9 @@ public: bool isDirty() const { return _isDirty; } - bool isReadOnly() const { return (_isUserReadOnly || _isFileReadOnly); }; + bool isReadOnly() const { return (_isUserReadOnly || _isFileReadOnly); } - bool isUntitled() const { return (_currentStatus == DOC_UNNAMED); } + bool isUntitled() const { return ((_currentStatus & DOC_UNNAMED) == DOC_UNNAMED); } bool getFileReadOnly() const { return _isFileReadOnly; } @@ -297,14 +297,14 @@ public: bool isLargeFile() const { return _isLargeFile; } - void startMonitoring() { - _isMonitoringOn = true; + void startMonitoring() { + _isMonitoringOn = true; _eventHandle = ::CreateEvent(nullptr, TRUE, FALSE, nullptr); }; HANDLE getMonitoringEvent() const { return _eventHandle; }; - void stopMonitoring() { + void stopMonitoring() { _isMonitoringOn = false; ::SetEvent(_eventHandle); ::CloseHandle(_eventHandle); diff --git a/PowerEditor/src/ScintillaComponent/SmartHighlighter.cpp b/PowerEditor/src/ScintillaComponent/SmartHighlighter.cpp index 159fedf07..239686ec7 100644 --- a/PowerEditor/src/ScintillaComponent/SmartHighlighter.cpp +++ b/PowerEditor/src/ScintillaComponent/SmartHighlighter.cpp @@ -33,10 +33,10 @@ void SmartHighlighter::highlightViewWithWord(ScintillaEditView * pHighlightView, auto originalEndPos = pHighlightView->execute(SCI_GETTARGETEND); // Get the range of text visible and highlight everything in it - auto firstLine = pHighlightView->execute(SCI_GETFIRSTVISIBLELINE); - auto nbLineOnScreen = pHighlightView->execute(SCI_LINESONSCREEN); - auto nbLines = min(nbLineOnScreen, MAXLINEHIGHLIGHT) + 1; - auto lastLine = firstLine + nbLines; + intptr_t firstLine = pHighlightView->execute(SCI_GETFIRSTVISIBLELINE); + intptr_t nbLineOnScreen = pHighlightView->execute(SCI_LINESONSCREEN); + intptr_t nbLines = min(nbLineOnScreen, MAXLINEHIGHLIGHT) + 1; + intptr_t lastLine = firstLine + nbLines; size_t startPos = 0; intptr_t endPos = 0; auto currentLine = firstLine; diff --git a/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp b/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp index b461c0d75..0adbf102e 100644 --- a/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp +++ b/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp @@ -1718,8 +1718,8 @@ void StringDlg::HandlePaste(HWND hEdit) void StylerDlg::move2CtrlRight(HWND hwndDlg, int ctrlID, HWND handle2Move, int handle2MoveWidth, int handle2MoveHeight) { - POINT p; - RECT rc; + POINT p{}; + RECT rc{}; ::GetWindowRect(::GetDlgItem(hwndDlg, ctrlID), &rc); p.x = rc.right + NppParameters::getInstance()._dpiManager.scaleX(5); @@ -1764,7 +1764,7 @@ intptr_t CALLBACK StylerDlg::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPA for (size_t j = 0 ; j < int(sizeof(fontSizeStrs))/(3*sizeof(TCHAR)) ; ++j) ::SendMessage(hFontSizeCombo, CB_ADDSTRING, 0, reinterpret_cast(fontSizeStrs[j])); - TCHAR size[10]; + TCHAR size[10] = { '\0' }; if (style._fontSize == -1) size[0] = '\0'; else @@ -1883,13 +1883,13 @@ intptr_t CALLBACK StylerDlg::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPA if (i != 0) { const size_t intStrLen = 3; - TCHAR intStr[intStrLen]; + TCHAR intStr[intStrLen] = { '\0' }; auto lbTextLen = ::SendDlgItemMessage(hwnd, LOWORD(wParam), CB_GETLBTEXTLEN, i, 0); if (static_cast(lbTextLen) > intStrLen - 1) return TRUE; ::SendDlgItemMessage(hwnd, LOWORD(wParam), CB_GETLBTEXT, i, reinterpret_cast(intStr)); - if ((!intStr) || (!intStr[0])) + if (!intStr[0]) style._fontSize = -1; else {