Addressing clang-tidy findings

This commit is contained in:
Dan Paulat 2025-08-24 22:28:38 -05:00
parent 193f42318c
commit 072865d2e5
5 changed files with 24 additions and 14 deletions

View file

@ -8,9 +8,13 @@ Checks:
- 'performance-*' - 'performance-*'
- '-bugprone-easily-swappable-parameters' - '-bugprone-easily-swappable-parameters'
- '-cppcoreguidelines-avoid-magic-numbers' - '-cppcoreguidelines-avoid-magic-numbers'
- '-cppcoreguidelines-avoid-do-while'
- '-cppcoreguidelines-avoid-non-const-global-variables'
- '-cppcoreguidelines-pro-type-reinterpret-cast' - '-cppcoreguidelines-pro-type-reinterpret-cast'
- '-cppcoreguidelines-pro-type-union-access'
- '-misc-include-cleaner' - '-misc-include-cleaner'
- '-misc-non-private-member-variables-in-classes' - '-misc-non-private-member-variables-in-classes'
- '-misc-use-anonymous-namespace'
- '-modernize-return-braced-init-list' - '-modernize-return-braced-init-list'
- '-modernize-use-trailing-return-type' - '-modernize-use-trailing-return-type'
FormatStyle: 'file' FormatStyle: 'file'

View file

@ -2,9 +2,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace scwx namespace scwx::network
{
namespace network
{ {
TEST(NtpClient, Poll) TEST(NtpClient, Poll)
@ -31,5 +29,4 @@ TEST(NtpClient, Poll)
!error); !error);
} }
} // namespace network } // namespace scwx::network
} // namespace scwx

View file

@ -23,8 +23,9 @@ public:
NtpClient(NtpClient&&) noexcept; NtpClient(NtpClient&&) noexcept;
NtpClient& operator=(NtpClient&&) noexcept; NtpClient& operator=(NtpClient&&) noexcept;
bool error(); bool error();
std::chrono::system_clock::duration time_offset() const;
[[nodiscard]] std::chrono::system_clock::duration time_offset() const;
void Start(); void Start();
void Stop(); void Stop();

View file

@ -56,7 +56,7 @@ public:
NtpTimestamp& operator=(NtpTimestamp&&) = default; NtpTimestamp& operator=(NtpTimestamp&&) = default;
template<typename Clock = std::chrono::system_clock> template<typename Clock = std::chrono::system_clock>
std::chrono::time_point<Clock> ToTimePoint() const [[nodiscard]] std::chrono::time_point<Clock> ToTimePoint() const
{ {
// Convert NTP seconds to Unix seconds // Convert NTP seconds to Unix seconds
// Don't cast to a larger type to account for rollover, and this should // 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 NtpClient::error()
{ {
bool returnValue = p->error_; const bool returnValue = p->error_;
p->error_ = false; p->error_ = false;
return returnValue; return returnValue;
} }
@ -273,6 +273,13 @@ void NtpClient::Impl::Poll()
static constexpr auto kTimeout_ = 5s; static constexpr auto kTimeout_ = 5s;
if (!serverEndpoint_.has_value())
{
logger_->error("Server endpoint not set");
error_ = true;
return;
}
try try
{ {
const auto originTimestamp = const auto originTimestamp =
@ -280,7 +287,7 @@ void NtpClient::Impl::Poll()
transmitPacket_.txTm_s = ntohl(originTimestamp.seconds_); transmitPacket_.txTm_s = ntohl(originTimestamp.seconds_);
transmitPacket_.txTm_f = ntohl(originTimestamp.fraction_); transmitPacket_.txTm_f = ntohl(originTimestamp.fraction_);
std::size_t transmitPacketSize = sizeof(transmitPacket_); const std::size_t transmitPacketSize = sizeof(transmitPacket_);
// Send NTP request // Send NTP request
socket_.send_to(boost::asio::buffer(&transmitPacket_, transmitPacketSize), socket_.send_to(boost::asio::buffer(&transmitPacket_, transmitPacketSize),
*serverEndpoint_); *serverEndpoint_);
@ -310,6 +317,7 @@ void NtpClient::Impl::Poll()
catch (const std::exception& ex) catch (const std::exception& ex)
{ {
logger_->error("Error polling: {}", ex.what()); logger_->error("Error polling: {}", ex.what());
error_ = true;
} }
} }
@ -437,7 +445,7 @@ void NtpClient::Impl::Run()
if (enabled_) if (enabled_)
{ {
std::chrono::seconds pollIntervalSeconds {1u << pollInterval_}; const std::chrono::seconds pollIntervalSeconds {1u << pollInterval_};
pollTimer_.expires_after(pollIntervalSeconds); pollTimer_.expires_after(pollIntervalSeconds);
pollTimer_.async_wait( pollTimer_.async_wait(
[this](const boost::system::error_code& e) [this](const boost::system::error_code& e)
@ -547,7 +555,7 @@ std::shared_ptr<NtpClient> NtpClient::Instance()
static std::weak_ptr<NtpClient> ntpClientReference_ {}; static std::weak_ptr<NtpClient> ntpClientReference_ {};
static std::mutex instanceMutex_ {}; static std::mutex instanceMutex_ {};
std::unique_lock lock(instanceMutex_); const std::unique_lock lock(instanceMutex_);
std::shared_ptr<NtpClient> ntpClient = ntpClientReference_.lock(); std::shared_ptr<NtpClient> ntpClient = ntpClientReference_.lock();

View file

@ -14,7 +14,7 @@ namespace scwx::types::ntp
NtpPacket NtpPacket::Parse(const std::span<std::uint8_t> data) NtpPacket NtpPacket::Parse(const std::span<std::uint8_t> data)
{ {
NtpPacket packet; NtpPacket packet {};
assert(data.size() >= sizeof(NtpPacket)); assert(data.size() >= sizeof(NtpPacket));