From dc3ffe33a23176a6e5ad3013de3de5f59e7b9318 Mon Sep 17 00:00:00 2001 From: Coelacanthus Date: Mon, 18 Jan 2021 22:06:59 +0800 Subject: [PATCH 1/2] chore: add clang-tidy to lint files --- .clang-tidy | 7 +++++++ CMakeLists.txt | 7 +++++++ makespec/BUILDVERSION | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 00000000..d08286e8 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,7 @@ +--- +Checks: '-*,bugprone-*,-bugprone-suspicious-include,-bugprone-narrowing-conversions,cppcoreguidelines-*,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-owning-memory,-cppcoreguidelines-macro-usage,-cppcoreguidelines-avoid-c-arrays,-cppcoreguidelines-pro-type-static-cast-downcast,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-special-member-functions,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-pro-type-vararg,modernize-*,-modernize-use-trailing-return-type,-modernize-use-auto,-modernize-avoid-c-arrays,performance-*,-performance-no-automatic-move,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-readability-implicit-bool-conversion,-readability-implicit-bool-conversion,-readability-redundant-access-specifiers,-readability-isolate-declaration,-readability-else-after-return,-readability-static-accessed-through-instance' +HeaderFilterRegex: '^src\/(?!forms)\/?.*$' +WarningsAsErrors: '*' +AnalyzeTemporaryDtors: false +FormatStyle: file +... diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ed47550..6e7cd494 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ option(BUILD_DEB "Build deb format package" OFF) LEMONLOG(BUILD_DEB) option(BUILD_RPM "Build rpm format package" OFF) LEMONLOG(BUILD_RPM) +option(USE_CLANG_TIDY "Use clang-tidy to lint the files" OFF) +LEMONLOG(USE_CLANG_TIDY) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTORCC ON) @@ -89,6 +91,11 @@ if(UNIX AND NOT APPLE) endif() LEMONLOG(LSB_RELEASE_ID_SHORT) +if(USE_CLANG_TIDY) + set(CMAKE_CXX_CLANG_TIDY "clang-tidy") + message(STATUS "clang-tidy checks will be performed") +endif() + # ================================================================================== # Lemon Build Info # ================================================================================== diff --git a/makespec/BUILDVERSION b/makespec/BUILDVERSION index 415196e4..078fa0fe 100644 --- a/makespec/BUILDVERSION +++ b/makespec/BUILDVERSION @@ -1 +1 @@ -118 +119 From 023c73f6385484f7e1e8726f3042a44d328be38c Mon Sep 17 00:00:00 2001 From: Coelacanthus Date: Mon, 18 Jan 2021 22:11:01 +0800 Subject: [PATCH 2/2] refactor: lint files --- makespec/BUILDVERSION | 2 +- src/base/LemonBaseApplication.cpp | 8 ++++---- src/base/LemonConfig.cpp | 1 + src/core/contestant.cpp | 7 ++++--- src/core/judgingthread.cpp | 23 ++++++++++++----------- src/core/testcase.cpp | 2 +- src/exttestcasetable.cpp | 2 +- src/exttestcaseupdaterdialog.cpp | 2 +- src/exttestcaseupdaterdialog.h | 2 +- src/summarytree.cpp | 2 +- 10 files changed, 27 insertions(+), 24 deletions(-) diff --git a/makespec/BUILDVERSION b/makespec/BUILDVERSION index 078fa0fe..52bd8e43 100644 --- a/makespec/BUILDVERSION +++ b/makespec/BUILDVERSION @@ -1 +1 @@ -119 +120 diff --git a/src/base/LemonBaseApplication.cpp b/src/base/LemonBaseApplication.cpp index 297f7920..448d06a2 100644 --- a/src/base/LemonBaseApplication.cpp +++ b/src/base/LemonBaseApplication.cpp @@ -19,15 +19,15 @@ using namespace Lemon; auto LemonBaseApplication::Initialize() -> bool { QString errorMessage; - bool canContinue; + bool canContinue = false; const auto hasError = parseCommandLine(&canContinue, &errorMessage); if (hasError) { // LOG("Command line: " + A(errorMessage)); - if (! canContinue) { + if (canContinue) { + LOG("Non-fatal error, LemonLime will continue starting up."); + } else { LOG("Fatal Error, LemonLime cannot continue."); return false; - } else { - LOG("Non-fatal error, LemonLime will continue starting up."); } } diff --git a/src/base/LemonConfig.cpp b/src/base/LemonConfig.cpp index c683bf4a..743fb828 100644 --- a/src/base/LemonConfig.cpp +++ b/src/base/LemonConfig.cpp @@ -34,6 +34,7 @@ namespace Lemon::base::config { QJsonArray _compilerList = json["compilerList"].toArray(); compilerList.clear(); compilerList.reserve(_compilerList.size()); + // NOLINTNEXTLINE for (int compilerIndex = 0; compilerIndex < _compilerList.size(); ++compilerIndex) { QJsonObject compilerObject = _compilerList[compilerIndex].toObject(); Compiler *compiler = new Compiler; diff --git a/src/core/contestant.cpp b/src/core/contestant.cpp index 30e50cb2..00e9cf37 100644 --- a/src/core/contestant.cpp +++ b/src/core/contestant.cpp @@ -235,7 +235,8 @@ void Contestant::readFromStream(QDataStream &in) { Qt::TimeSpec(judgingTime_timespec)); int count = 0; int _count = 0; - int __count = 0; + int ___count = 0; // NOLINT + // FIXME: using semantic variables int tmp = 0; in >> count; @@ -252,9 +253,9 @@ void Contestant::readFromStream(QDataStream &in) { for (int j = 0; j < _count; j++) { result[i].append(QList()); - in >> __count; + in >> ___count; - for (int k = 0; k < __count; k++) { + for (int k = 0; k < ___count; k++) { in >> tmp; result[i][j].append(ResultState(tmp)); } diff --git a/src/core/judgingthread.cpp b/src/core/judgingthread.cpp index ddcc2e6f..2f6c756d 100644 --- a/src/core/judgingthread.cpp +++ b/src/core/judgingthread.cpp @@ -26,6 +26,7 @@ #include #endif +// NOLINTNEXTLINE JudgingThread::JudgingThread(QObject *parent) : QThread(parent) { moveToThread(this); // checkRejudgeMode = false; @@ -148,10 +149,10 @@ void JudgingThread::compareLineByLine(const QString &contestantOutput) { if (chk1) chk1 = false; - str1[len1++] = ch; + str1[len1++] = ch; // NOLINT } - str1[len1++] = '\0'; + str1[len1++] = '\0'; // NOLINT chkEof1 = ch == EOF; len2 = 0; @@ -185,10 +186,10 @@ void JudgingThread::compareLineByLine(const QString &contestantOutput) { if (chk2) chk2 = false; - str2[len2++] = ch; + str2[len2++] = ch; // NOLINT } - str2[len2++] = '\0'; + str2[len2++] = '\0'; // NOLINT chkEof2 = ch == EOF; if (len1 != len2 || strcmp(str1, str2) != 0) { @@ -395,7 +396,7 @@ void JudgingThread::compareIgnoreSpaces(const QString &contestantOutput) { while (len1 < 20) { if (ch1 != ' ' && ch1 != '\t' && ch1 != '\n' && ch1 != '\r' && ch1 != EOF) { - str1[len1++] = ch1; + str1[len1++] = ch1; // NOLINT } else { break; } @@ -403,12 +404,12 @@ void JudgingThread::compareIgnoreSpaces(const QString &contestantOutput) { ch1 = static_cast(fgetc(contestantOutputFile)); } - str1[len1] = '\0'; + str1[len1] = '\0'; // NOLINT int len2 = 0; while (len2 < 20) { if (ch2 != ' ' && ch2 != '\t' && ch2 != '\n' && ch2 != '\r' && ch2 != EOF) { - str2[len2++] = ch2; + str2[len2++] = ch2; // NOLINT } else { break; } @@ -416,7 +417,7 @@ void JudgingThread::compareIgnoreSpaces(const QString &contestantOutput) { ch2 = static_cast(fgetc(standardOutputFile)); } - str2[len2] = '\0'; + str2[len2] = '\0'; // NOLINT if (len1 != len2 || strcmp(str1, str2) != 0) { if (len1 <= 0) { @@ -511,7 +512,7 @@ void JudgingThread::compareRealNumbers(const QString &contestantOutput) { int cnt1 = fscanf(contestantOutputFile, "%Lf", &a); int cnt2 = fscanf(standardOutputFile, "%Lf", &b); - char temps = fgetc(standardOutputFile); + char temps = static_cast(fgetc(standardOutputFile)); if (cnt1 == 0) { score = 0; @@ -557,8 +558,8 @@ void JudgingThread::compareRealNumbers(const QString &contestantOutput) { score = 0; result = WrongAnswer; message = tr(R"(On line %3, Read "%1" but expect "%2")") - .arg(a, 0, 'g', 18) - .arg(b, 0, 'g', 18) + .arg(a, 0, 'g', 18) // NOLINT + .arg(b, 0, 'g', 18) // NOLINT .arg(nowRow); fclose(contestantOutputFile); fclose(standardOutputFile); diff --git a/src/core/testcase.cpp b/src/core/testcase.cpp index a09cc57b..1c912e25 100644 --- a/src/core/testcase.cpp +++ b/src/core/testcase.cpp @@ -144,7 +144,7 @@ void TestCase::readFromStream(QDataStream &in) { if (i.endsWith("_lemon_SUbtaskDEPENDENCE_fLAg")) { int temp(0); - for (auto *itr = i.constBegin(); *itr != '_'; ++itr) + for (const auto *itr = i.constBegin(); *itr != '_'; ++itr) (temp *= 10) += itr->toLatin1() ^ '0'; dependenceSubtask.push_back(temp); diff --git a/src/exttestcasetable.cpp b/src/exttestcasetable.cpp index 40412502..10c60a62 100644 --- a/src/exttestcasetable.cpp +++ b/src/exttestcasetable.cpp @@ -244,7 +244,7 @@ void ExtTestCaseTable::whenItemSelectionChanged() { if (haveSub.size() >= 2 && resSub.empty()) isCanMerge = true; - if (haveSub.size() >= 1 && resSub.empty()) + if (haveSub.size() >= 1 && resSub.empty()) // NOLINT isCanSplit = true; emit testCaseSelectionChanged(); diff --git a/src/exttestcaseupdaterdialog.cpp b/src/exttestcaseupdaterdialog.cpp index 80d9cbd6..c008f746 100644 --- a/src/exttestcaseupdaterdialog.cpp +++ b/src/exttestcaseupdaterdialog.cpp @@ -18,7 +18,7 @@ ExtTestCaseUpdaterDialog::ExtTestCaseUpdaterDialog(QWidget *parent, Task *nowTask, const Settings *nowSettings, int nowCaseNumber, int editScore, int editData, int editTime, int editMemory, - int editDepend, QList tempDepends) + int editDepend, const QList &tempDepends) : QDialog(parent), ui(new Ui::ExtTestCaseUpdaterDialog), nowTask(nowTask), nowSettings(nowSettings), nowCaseNumber(nowCaseNumber), editScore(editScore), editData(editData), editTime(editTime), editMemory(editMemory), editDepend(editDepend) { diff --git a/src/exttestcaseupdaterdialog.h b/src/exttestcaseupdaterdialog.h index 6491579b..cdcf0a90 100644 --- a/src/exttestcaseupdaterdialog.h +++ b/src/exttestcaseupdaterdialog.h @@ -29,7 +29,7 @@ class ExtTestCaseUpdaterDialog : public QDialog { const Settings *nowSettings = nullptr, int nowCaseNumber = 0, int editScore = NO_EDIT, int editData = NO_EDIT, int editTime = NO_EDIT, int editMemory = NO_EDIT, int editDepend = NO_EDIT, - QList tempDepends = QList()); + const QList &tempDepends = QList()); ~ExtTestCaseUpdaterDialog(); int getScore() const; diff --git a/src/summarytree.cpp b/src/summarytree.cpp index ed47743d..21052737 100644 --- a/src/summarytree.cpp +++ b/src/summarytree.cpp @@ -33,7 +33,7 @@ SummaryTree::SummaryTree(QWidget *parent) : QTreeWidget(parent) { addTestCaseKeyAction->setShortcutContext(Qt::WidgetShortcut); deleteTaskKeyAction->setShortcutContext(Qt::WidgetShortcut); deleteTestCaseKeyAction->setShortcutContext(Qt::WidgetShortcut); - addTaskKeyAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Insert)); + addTaskKeyAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Insert)); // NOLINT: Qt issue addTestCaseKeyAction->setShortcut(QKeySequence(Qt::Key_Insert)); deleteTaskKeyAction->setShortcut(QKeySequence(Qt::Key_Delete)); deleteTestCaseKeyAction->setShortcut(QKeySequence(Qt::Key_Delete));