From f3564ac6b9b1d4bed1d5338e3689268419c773bc Mon Sep 17 00:00:00 2001 From: niansa/tuxifan Date: Fri, 2 Jun 2023 21:46:41 +0200 Subject: [PATCH] Fixed tons of warnings and clazy findings (#811) --- gpt4all-backend/gptj.cpp | 2 +- gpt4all-backend/llamamodel.cpp | 2 +- gpt4all-backend/mpt.cpp | 2 +- gpt4all-chat/chat.cpp | 4 ++-- gpt4all-chat/chatlistmodel.cpp | 4 ++-- gpt4all-chat/chatllm.cpp | 1 - gpt4all-chat/chatmodel.h | 2 +- gpt4all-chat/database.cpp | 19 +++++++++++-------- gpt4all-chat/download.cpp | 5 ++--- gpt4all-chat/network.cpp | 7 ++++--- gpt4all-chat/server.cpp | 2 +- gpt4all-chat/sysinfo.h | 3 ++- 12 files changed, 28 insertions(+), 25 deletions(-) diff --git a/gpt4all-backend/gptj.cpp b/gpt4all-backend/gptj.cpp index 83855ed7..f0ce58a2 100644 --- a/gpt4all-backend/gptj.cpp +++ b/gpt4all-backend/gptj.cpp @@ -1013,7 +1013,7 @@ void GPTJ::prompt(const std::string &prompt, return; // Check if it partially matches our reverse prompts and if so, cache - for (auto s : reversePrompts) { + for (const auto &s : reversePrompts) { if (s.compare(0, completed.size(), completed) == 0) { foundPartialReversePrompt = true; cachedResponse = completed; diff --git a/gpt4all-backend/llamamodel.cpp b/gpt4all-backend/llamamodel.cpp index 86a7cc63..fd2e8fc9 100644 --- a/gpt4all-backend/llamamodel.cpp +++ b/gpt4all-backend/llamamodel.cpp @@ -278,7 +278,7 @@ void LLamaModel::prompt(const std::string &prompt, } // Check if it partially matches our reverse prompts and if so, cache - for (auto s : reversePrompts) { + for (const auto &s : reversePrompts) { if (s.compare(0, completed.size(), completed) == 0) { foundPartialReversePrompt = true; cachedResponse = completed; diff --git a/gpt4all-backend/mpt.cpp b/gpt4all-backend/mpt.cpp index b37dc2b6..4145c9c2 100644 --- a/gpt4all-backend/mpt.cpp +++ b/gpt4all-backend/mpt.cpp @@ -942,7 +942,7 @@ void MPT::prompt(const std::string &prompt, return; // Check if it partially matches our reverse prompts and if so, cache - for (auto s : reversePrompts) { + for (const auto &s : reversePrompts) { if (s.compare(0, completed.size(), completed) == 0) { foundPartialReversePrompt = true; cachedResponse = completed; diff --git a/gpt4all-chat/chat.cpp b/gpt4all-chat/chat.cpp index 3ae25616..a57f4997 100644 --- a/gpt4all-chat/chat.cpp +++ b/gpt4all-chat/chat.cpp @@ -387,7 +387,7 @@ QList Chat::modelList() const QDir dir(exePath); dir.setNameFilters(QStringList() << "ggml-*.bin"); QStringList fileNames = dir.entryList(); - for (QString f : fileNames) { + for (const QString& f : fileNames) { QString filePath = exePath + f; QFileInfo info(filePath); QString name = info.completeBaseName().remove(0, 5); @@ -404,7 +404,7 @@ QList Chat::modelList() const QDir dir(localPath); dir.setNameFilters(QStringList() << "ggml-*.bin" << "chatgpt-*.txt"); QStringList fileNames = dir.entryList(); - for (QString f : fileNames) { + for (const QString &f : fileNames) { QString filePath = localPath + f; QFileInfo info(filePath); QString basename = info.completeBaseName(); diff --git a/gpt4all-chat/chatlistmodel.cpp b/gpt4all-chat/chatlistmodel.cpp index 97cd018c..a794284d 100644 --- a/gpt4all-chat/chatlistmodel.cpp +++ b/gpt4all-chat/chatlistmodel.cpp @@ -118,7 +118,7 @@ void ChatsRestoreThread::run() QDir dir(settingsPath); dir.setNameFilters(QStringList() << "gpt4all-*.chat"); QStringList fileNames = dir.entryList(); - for (QString f : fileNames) { + for (const QString &f : fileNames) { QString filePath = settingsPath + "/" + f; QFile file(filePath); bool success = file.open(QIODevice::ReadOnly); @@ -140,7 +140,7 @@ void ChatsRestoreThread::run() QDir dir(savePath); dir.setNameFilters(QStringList() << "gpt4all-*.chat"); QStringList fileNames = dir.entryList(); - for (QString f : fileNames) { + for (const QString &f : fileNames) { QString filePath = savePath + "/" + f; QFile file(filePath); bool success = file.open(QIODevice::ReadOnly); diff --git a/gpt4all-chat/chatllm.cpp b/gpt4all-chat/chatllm.cpp index 4804d492..7666fddc 100644 --- a/gpt4all-chat/chatllm.cpp +++ b/gpt4all-chat/chatllm.cpp @@ -546,7 +546,6 @@ bool ChatLLM::handleNameResponse(int32_t token, const std::string &response) emit generatedNameChanged(); QString gen = QString::fromStdString(m_nameResponse).simplified(); QStringList words = gen.split(' ', Qt::SkipEmptyParts); - int wordCount = words.size(); return words.size() <= 3; } diff --git a/gpt4all-chat/chatmodel.h b/gpt4all-chat/chatmodel.h index 195a4887..2ff4f388 100644 --- a/gpt4all-chat/chatmodel.h +++ b/gpt4all-chat/chatmodel.h @@ -240,7 +240,7 @@ public: bool serialize(QDataStream &stream, int version) const { stream << count(); - for (auto c : m_chatItems) { + for (const auto &c : m_chatItems) { stream << c.id; stream << c.name; stream << c.value; diff --git a/gpt4all-chat/database.cpp b/gpt4all-chat/database.cpp index 7ec2050a..1bb96115 100644 --- a/gpt4all-chat/database.cpp +++ b/gpt4all-chat/database.cpp @@ -125,12 +125,13 @@ bool removeChunksByDocumentId(QSqlQuery &q, int document_id) QStringList generateGrams(const QString &input, int N) { // Remove common English punctuation using QRegularExpression - QRegularExpression punctuation(R"([.,;:!?'"()\-])"); + static QRegularExpression punctuation(R"([.,;:!?'"()\-])"); QString cleanedInput = input; cleanedInput = cleanedInput.remove(punctuation); // Split the cleaned input into words using whitespace - QStringList words = cleanedInput.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts); + static QRegularExpression spaces("\\s+"); + QStringList words = cleanedInput.split(spaces, Qt::SkipEmptyParts); N = qMin(words.size(), N); // Generate all possible N-grams @@ -147,7 +148,8 @@ QStringList generateGrams(const QString &input, int N) bool selectChunk(QSqlQuery &q, const QList &collection_names, const QString &chunk_text, int retrievalSize) { - const int N_WORDS = chunk_text.split(QRegularExpression("\\s+")).size(); + static QRegularExpression spaces("\\s+"); + const int N_WORDS = chunk_text.split(spaces).size(); for (int N = N_WORDS; N > 2; N--) { // first try trigrams QList text = generateGrams(chunk_text, N); @@ -730,7 +732,7 @@ void Database::addCurrentFolders() return; } - for (auto i : collections) + for (const auto &i : collections) addFolder(i.collection, i.folder_path); } @@ -839,7 +841,7 @@ void Database::removeFolderInternal(const QString &collection, int folder_id, co // First remove all upcoming jobs associated with this folder by performing an opt-in filter QQueue docsToScan; - for (DocumentInfo info : m_docsToScan) { + for (const DocumentInfo &info : m_docsToScan) { if (info.folder == folder_id) continue; docsToScan.append(info); @@ -906,9 +908,11 @@ void Database::retrieveFromDB(const QList &collections, const QString & } while (q.next()) { +#if defined(DEBUG) const int rowid = q.value(0).toInt(); - const QString date = QDateTime::fromMSecsSinceEpoch(q.value(1).toLongLong()).toString("yyyy, MMMM dd"); +#endif const QString chunk_text = q.value(2).toString(); + const QString date = QDateTime::fromMSecsSinceEpoch(q.value(1).toLongLong()).toString("yyyy, MMMM dd"); const QString file = q.value(3).toString(); const QString title = q.value(4).toString(); const QString author = q.value(5).toString(); @@ -946,7 +950,7 @@ void Database::cleanDB() return; } - for (auto i : collections) { + for (const auto &i : collections) { // Find the path for the folder QFileInfo info(i.folder_path); if (!info.exists() || !info.isReadable()) { @@ -1017,7 +1021,6 @@ void Database::changeChunkSize(int chunkSize) while (q.next()) { int document_id = q.value(0).toInt(); - QString document_path = q.value(1).toString(); // Remove all chunks and documents to change the chunk size QSqlQuery query; if (!removeChunksByDocumentId(query, document_id)) { diff --git a/gpt4all-chat/download.cpp b/gpt4all-chat/download.cpp index a8419fd8..90acecf9 100644 --- a/gpt4all-chat/download.cpp +++ b/gpt4all-chat/download.cpp @@ -74,7 +74,7 @@ QList Download::modelList() const ModelInfo bestLlamaInfo; ModelInfo bestMPTInfo; QList filtered; - for (ModelInfo v : values) { + for (const ModelInfo &v : values) { if (v.isDefault) defaultInfo = v; if (v.bestGPTJ) @@ -310,7 +310,7 @@ void Download::removeModel(const QString &modelFile) void Download::handleSslErrors(QNetworkReply *reply, const QList &errors) { QUrl url = reply->request().url(); - for (auto e : errors) + for (const auto &e : errors) qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url; } @@ -658,7 +658,6 @@ void Download::handleReadyRead() if (!modelReply) return; - QString modelFilename = modelReply->url().fileName(); QFile *tempFile = m_activeDownloads.value(modelReply); QByteArray buffer; while (!modelReply->atEnd()) { diff --git a/gpt4all-chat/network.cpp b/gpt4all-chat/network.cpp index 75167570..3c003973 100644 --- a/gpt4all-chat/network.cpp +++ b/gpt4all-chat/network.cpp @@ -153,15 +153,16 @@ void Network::handleJsonUploadFinished() sendHealth(); } +#if defined(DEBUG) QByteArray jsonData = jsonReply->readAll(); QJsonParseError err; + QJsonDocument document = QJsonDocument::fromJson(jsonData, &err); if (err.error != QJsonParseError::NoError) { qDebug() << "ERROR: Couldn't parse: " << jsonData << err.errorString(); return; } -#if defined(DEBUG) printf("%s\n", qPrintable(document.toJson(QJsonDocument::Indented))); fflush(stdout); #endif @@ -172,7 +173,7 @@ void Network::handleJsonUploadFinished() void Network::handleSslErrors(QNetworkReply *reply, const QList &errors) { QUrl url = reply->request().url(); - for (auto e : errors) + for (const auto &e : errors) qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url; } @@ -421,7 +422,7 @@ void Network::sendMixpanelEvent(const QString &ev, const QVector &valu #endif } - for (auto p : values) + for (const auto& p : values) properties.insert(p.key, p.value); QJsonObject event; diff --git a/gpt4all-chat/server.cpp b/gpt4all-chat/server.cpp index 87ba5cde..2a65546c 100644 --- a/gpt4all-chat/server.cpp +++ b/gpt4all-chat/server.cpp @@ -192,7 +192,7 @@ QHttpServerResponse Server::handleCompletionRequest(const QHttpServerRequest &re prompts.append(promptValue.toString()); else { QJsonArray array = promptValue.toArray(); - for (QJsonValue v : array) + for (const QJsonValue &v : array) prompts.append(v.toString()); } } else diff --git a/gpt4all-chat/sysinfo.h b/gpt4all-chat/sysinfo.h index 4a02826f..29a3b9bd 100644 --- a/gpt4all-chat/sysinfo.h +++ b/gpt4all-chat/sysinfo.h @@ -24,7 +24,8 @@ QString getSystemTotalRAM() QString line = in.readLine(); while (!line.isNull()) { if (line.startsWith("MemTotal")) { - QStringList parts = line.split(QRegularExpression("\\s+")); + static QRegularExpression spaces("\\s+"); + QStringList parts = line.split(spaces); totalRAM = parts[1].toLongLong() * 1024; // Convert from KB to bytes break; }