Replace C-style casts with static_cast/reinterpret_cast for -Wold-style-cast compliance#445
Conversation
…le-cast compliance Projects using -Wold-style-cast and -Wuseless-cast (common in modern C++ codebases with strict warning policies) cannot compile concurrentqueue without warnings. Changes: - lightweightsemaphore.h: Replace C-style casts with static_cast in timed_wait, waitWithPartialSpinning, waitManyWithPartialSpinning, and signal methods - blockingconcurrentqueue.h: Replace C-style casts with static_cast for sema->signal, sema->waitMany, sema->tryWaitMany, sema->availableApprox calls and constructor MAX_SEMA_SPINS; replace C-style pointer casts in assert with reinterpret_cast - concurrentqueue.h: Suppress -Wuseless-cast around hash_thread_id static_cast that is needed for cross-platform correctness but redundant on 64-bit Tested clean with GCC 14/15, C++17/20/23, using -Wall -Wextra -Wpedantic -Werror -Wold-style-cast -Wuseless-cast -Wconversion -Wsign-conversion and other strict flags.
cameron314
left a comment
There was a problem hiding this comment.
Personally, I think the code is less readable when using static_cast for integral conversions. However, it's probably more important to avoid warnings.
blockingconcurrentqueue.h
Outdated
| { | ||
| if ((details::likely)(inner.enqueue_bulk(std::forward<It>(itemFirst), count))) { | ||
| sema->signal((LightweightSemaphore::ssize_t)(ssize_t)count); | ||
| sema->signal(static_cast<LightweightSemaphore::ssize_t>(count)); |
There was a problem hiding this comment.
This is not equivalent when sizeof(BlockingConcurrentQueue::ssize_t) != sizeof(LightweightSemaphore::ssize_t).
If (ssize_t)count is not negative (and it shouldn't be here!) then this works. But I'd still prefer an assert rather than an implicit assumption.
There was a problem hiding this comment.
I've restored the two-step cast through ssize_t and added assert(static_cast<ssize_t>(count) >= 0) before each conversion. Applied consistently across all 10 call sites in the file.
…ith assert Address maintainer review: restore the intermediate ssize_t cast that was dropped when converting from C-style casts, and add an explicit assert that the value is non-negative to guard against size differences between BlockingConcurrentQueue::ssize_t and LightweightSemaphore::ssize_t.
|
Hi @cameron314, just a friendly ping — this PR has been approved and looks ready to go. Would you be able to merge it when you get a chance? Thank you! |
Summary
Fixes #444
Replace all C-style casts with
static_cast/reinterpret_castacross the three public headers, enabling compilation with-Wold-style-cast -Wuseless-cast -Werroron GCC 14/15.lightweightsemaphore.h(5 casts):(time_t)→static_cast<time_t>,(long)→static_cast<long>,(std::uint64_t)→static_cast<std::uint64_t>,(int)→static_cast<int>blockingconcurrentqueue.h(15 casts): All(size_t),(ssize_t),(int),(LightweightSemaphore::ssize_t)→static_cast<>. Assert(BlockingConcurrentQueue*)1→reinterpret_cast<>concurrentqueue.h(1 cast): Suppress-Wuseless-castaroundhash_thread_id— thestatic_cast<size_t>is needed for 32-bit platforms but redundant on 64-bitTest plan
-std=c++23 -Wall -Wextra -Wpedantic -Werror -Wold-style-cast -Wuseless-cast -Wconversion -Wsign-conversion— cleanTest results
All tests executed inside Docker (gcc:latest, GCC 15.2.0):
Unit tests (make tests && bin/unittests)
All tests passed:
Fuzz tests (bin/fuzztests)
700+ iterations executed, 0 failures: