From 072865d2e5277048abac77d1c4da3091950760cc Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 24 Aug 2025 22:28:38 -0500 Subject: [PATCH] Addressing clang-tidy findings --- test/.clang-tidy | 4 ++++ test/source/scwx/network/ntp_client.test.cpp | 7 ++----- wxdata/include/scwx/network/ntp_client.hpp | 5 +++-- wxdata/source/scwx/network/ntp_client.cpp | 20 ++++++++++++++------ wxdata/source/scwx/types/ntp_types.cpp | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/test/.clang-tidy b/test/.clang-tidy index d5079a03..254b44ad 100644 --- a/test/.clang-tidy +++ b/test/.clang-tidy @@ -8,9 +8,13 @@ Checks: - 'performance-*' - '-bugprone-easily-swappable-parameters' - '-cppcoreguidelines-avoid-magic-numbers' + - '-cppcoreguidelines-avoid-do-while' + - '-cppcoreguidelines-avoid-non-const-global-variables' - '-cppcoreguidelines-pro-type-reinterpret-cast' + - '-cppcoreguidelines-pro-type-union-access' - '-misc-include-cleaner' - '-misc-non-private-member-variables-in-classes' + - '-misc-use-anonymous-namespace' - '-modernize-return-braced-init-list' - '-modernize-use-trailing-return-type' FormatStyle: 'file' diff --git a/test/source/scwx/network/ntp_client.test.cpp b/test/source/scwx/network/ntp_client.test.cpp index bdfcb4ae..1450b324 100644 --- a/test/source/scwx/network/ntp_client.test.cpp +++ b/test/source/scwx/network/ntp_client.test.cpp @@ -2,9 +2,7 @@ #include -namespace scwx -{ -namespace network +namespace scwx::network { TEST(NtpClient, Poll) @@ -31,5 +29,4 @@ TEST(NtpClient, Poll) !error); } -} // namespace network -} // namespace scwx +} // namespace scwx::network diff --git a/wxdata/include/scwx/network/ntp_client.hpp b/wxdata/include/scwx/network/ntp_client.hpp index beb4be21..8be912b3 100644 --- a/wxdata/include/scwx/network/ntp_client.hpp +++ b/wxdata/include/scwx/network/ntp_client.hpp @@ -23,8 +23,9 @@ public: NtpClient(NtpClient&&) noexcept; NtpClient& operator=(NtpClient&&) noexcept; - bool error(); - std::chrono::system_clock::duration time_offset() const; + bool error(); + + [[nodiscard]] std::chrono::system_clock::duration time_offset() const; void Start(); void Stop(); diff --git a/wxdata/source/scwx/network/ntp_client.cpp b/wxdata/source/scwx/network/ntp_client.cpp index e95599dd..494aadde 100644 --- a/wxdata/source/scwx/network/ntp_client.cpp +++ b/wxdata/source/scwx/network/ntp_client.cpp @@ -56,7 +56,7 @@ public: NtpTimestamp& operator=(NtpTimestamp&&) = default; template - std::chrono::time_point ToTimePoint() const + [[nodiscard]] std::chrono::time_point ToTimePoint() const { // Convert NTP seconds to Unix seconds // Don't cast to a larger type to account for rollover, and this should @@ -191,8 +191,8 @@ NtpClient::Impl::~Impl() bool NtpClient::error() { - bool returnValue = p->error_; - p->error_ = false; + const bool returnValue = p->error_; + p->error_ = false; return returnValue; } @@ -273,6 +273,13 @@ void NtpClient::Impl::Poll() static constexpr auto kTimeout_ = 5s; + if (!serverEndpoint_.has_value()) + { + logger_->error("Server endpoint not set"); + error_ = true; + return; + } + try { const auto originTimestamp = @@ -280,7 +287,7 @@ void NtpClient::Impl::Poll() transmitPacket_.txTm_s = ntohl(originTimestamp.seconds_); transmitPacket_.txTm_f = ntohl(originTimestamp.fraction_); - std::size_t transmitPacketSize = sizeof(transmitPacket_); + const std::size_t transmitPacketSize = sizeof(transmitPacket_); // Send NTP request socket_.send_to(boost::asio::buffer(&transmitPacket_, transmitPacketSize), *serverEndpoint_); @@ -310,6 +317,7 @@ void NtpClient::Impl::Poll() catch (const std::exception& ex) { logger_->error("Error polling: {}", ex.what()); + error_ = true; } } @@ -437,7 +445,7 @@ void NtpClient::Impl::Run() if (enabled_) { - std::chrono::seconds pollIntervalSeconds {1u << pollInterval_}; + const std::chrono::seconds pollIntervalSeconds {1u << pollInterval_}; pollTimer_.expires_after(pollIntervalSeconds); pollTimer_.async_wait( [this](const boost::system::error_code& e) @@ -547,7 +555,7 @@ std::shared_ptr NtpClient::Instance() static std::weak_ptr ntpClientReference_ {}; static std::mutex instanceMutex_ {}; - std::unique_lock lock(instanceMutex_); + const std::unique_lock lock(instanceMutex_); std::shared_ptr ntpClient = ntpClientReference_.lock(); diff --git a/wxdata/source/scwx/types/ntp_types.cpp b/wxdata/source/scwx/types/ntp_types.cpp index 9ff39095..daf5d46b 100644 --- a/wxdata/source/scwx/types/ntp_types.cpp +++ b/wxdata/source/scwx/types/ntp_types.cpp @@ -14,7 +14,7 @@ namespace scwx::types::ntp NtpPacket NtpPacket::Parse(const std::span data) { - NtpPacket packet; + NtpPacket packet {}; assert(data.size() >= sizeof(NtpPacket));